读《Introducing Go》O'Reilly 第 5 章
来源:互联网 发布:百度网络推广 编辑:程序博客网 时间:2024/04/28 04:21
第5章,数组,切片,字典,33页
数组是同一个长度类型顺序存储的元素集。
var x [5]int
func main() {
var x [5]int
x[4] = 100
fmt.Println(x)
}
数组下标从0 开始。
上面代码输出:
[0 0 0 0 100]
var x [5]float64
x[0] = 98
x[1] = 93
x[2] = 77
x[3] = 82
x[4] = 83
var total float64 = 0
for i := 0; i<5; i++ {
total += x[i]
}
fmt.Println(total/5)
x[0] = 98
x[1] = 93
x[2] = 77
x[3] = 82
x[4] = 83
var total float64 = 0
for i := 0; i<5; i++ {
total += x[i]
}
fmt.Println(total/5)
上面代码:计算数组中五个数的平均值。
var x [5]float64
x[0] = 98
x[1] = 93
x[2] = 77
x[3] = 82
x[4] = 83
var total float64 = 0
for i:=0; i<len(x); i++ {
total += x[i]
}
fmt.Println( reflect.TypeOf( len(x) ) )
// len() 方法返回 int 类型值。
fmt.Println(total/len(x))
x[0] = 98
x[1] = 93
x[2] = 77
x[3] = 82
x[4] = 83
var total float64 = 0
for i:=0; i<len(x); i++ {
total += x[i]
}
fmt.Println( reflect.TypeOf( len(x) ) )
// len() 方法返回 int 类型值。
fmt.Println(total/len(x))
上面代码会报:.\floatcal.go:26: invalid operation: total / len(x) (mismatched types float64 and int)
意思:float 类型除以 int 类型出错。
reflect.TypeOf 方法可返回变量或表达式的类型。
要 import "reflect"
import (
"fmt"
"reflect"
)
"fmt"
"reflect"
)
fmt.Println(total/ float64(len(x)) )
强制类型转换一下就可以了。
var total float64 = 0
for _, value := range x {
total += value
}
fmt.Println(total / float64( len(x) ) )
for _, value := range x {
total += value
}
fmt.Println(total / float64( len(x) ) )
golang 有的类库没有使用到也不可以,还会报错。
_ 可以不用迭代器,即变量 i 来循环数组,即不使用循环计数器。
初始化数组写法:
x := [5]float64{ 98, 93, 77, 82, 83 }
或
x := [5]float64{
98,
93,
83,
77,
82,
}
a := [2][2]int{ {1,2}, {3,4} }
slice 切片
类似数组,可以改变长度。切片是引用方式。
make来初始化,可以完成内部指针初始化,其后可以立即使用
跟 new 不同的是,make 返回类型的引用而不是指针,而返回值也依赖于具体传入的类型
在go语言中,有4种引用类型:slice,map,channel,interface。
Slice,map,channel一般都通过make进行初始化:
ci := make(chan int) // unbuffered channel of integers 无缓存整数通道
cj := make(chan int, 0) // unbuffered channel of integers
cs := make(chan *os.File, 100) // buffered channel of pointers to Files
第二个参数是初始化切片,数组长度。第三个参数是预留长度。
var x [5]float64
x[0] = 98
x[1] = 93
x[2] = 77
x[3] = 82
x[4] = 83
// y := make([]float64, 5)
z := make([]float64, 2, 3)
z1 := append(z, 1, 2, 3, 4)
fmt.Println(len(x))
fmt.Println(x)
fmt.Println(len(z))
fmt.Println(z)
fmt.Println(z1)
x[0] = 98
x[1] = 93
x[2] = 77
x[3] = 82
x[4] = 83
// y := make([]float64, 5)
z := make([]float64, 2, 3)
z1 := append(z, 1, 2, 3, 4)
fmt.Println(len(x))
fmt.Println(x)
fmt.Println(len(z))
fmt.Println(z)
fmt.Println(z1)
不过我追加4个元素也进去了。那3 是给谁预留的呢?
按文档讲,x := make([]float64, 5, 10)
指 x 共有 10 个元素位置。用了或初始化 5 个。
还有 5 个位置可用。
另一种创建切片的方法:通过数组来创建。
arr := [5]float64{1, 2, 3, 4, 5}
x := arr[1:4]
arr[1:4] 的意思是:[2, 3, 4] ,注意[low, high] 中 high 不含自己即 low >= 1, high < 4
append(arr1, arr2)
append(arr1, 1, 2, 3, 4)
二种写法追加数组
x := []float64{1, 2, 3, 4, 5}
y := make([]float64, 2)
fmt.Println(y)
// copy(y, x)
copy(x, y)
fmt.Println(x, y)
y := make([]float64, 2)
fmt.Println(y)
// copy(y, x)
copy(x, y)
fmt.Println(x, y)
输出:
[0 0]
[0 0 3 4 5] [0 0]
[0 0 3 4 5] [0 0]
y, 向 x 复制
[0 0]
[1 2 3 4 5] [1 2]
[1 2 3 4 5] [1 2]
字典 Map 38页
是无序键值对的集合(也称地图,关联数组,哈希表或字典)。
网络资源文章:http://studygolang.com/articles/2494
字典实现哈希表的基本功能(hash )元素的查询,添加和删除
go 是强类型语言,必须为 map 的键和值都指定类型
var x map[string]int
m := make(map[string]string)
m["bandName"] = "Funny Bones" // "create"websiteTitle := m["bandName"] + " Music" // "read"m["bandName"] = "Moon Taxi" // "update"delete(m, "bandName") // "delete"fmt.Printf(m["bandName"]) // prints nothing since m["bandName"] == ""
遍历
for key, value := range m { fmt.Println("Key:", key, "Value:", value)}
扩展阅读:http://blog.csdn.net/abv123456789/article/category/2206183
0 0
- 读《Introducing Go》O'Reilly 第 5 章
- 读《Introducing Go》O'Reilly , 第1,2章
- 读《Introducing Go》O'Reilly,第3,4章
- 读《Introducing Go》O'Reilly 第 6 ~ 11 章
- Introducing Go - O'Reilly 2016 阅读笔记
- O'Reilly
- O'reilly
- O'Reilly
- O'Reilly Hacks Ebooks
- O'Reilly Unicode Explained
- O'reilly Firefox Hacks
- O'Reilly PSP Hacks
- O'Reilly 'Hacks' Various
- O'Reilly书名
- O'Reilly Ebooks 大全
- O'Reilly Learning OpenCV
- O'Reilly 免费电子书
- 翻译- O'Reilly ZooKeeper-第二章 开始掌握ZooKeeper
- HTML5打开本地app(应用)的方法
- HDU2191 多重背包
- Hdoj-1074-状压dp
- 【Hadoop 10周年】我与Hadoop不得不说的故事
- Floating-point manipulation functions 浮点处理功能
- 读《Introducing Go》O'Reilly 第 5 章
- 【leetCode-62】Unique Paths
- eclipse An error has occurred,See error log for more details”错误的解决方法
- 常见MFC UI界面库
- xmPP的简单了解及使用(4)
- 如何将word在网页中展现为HTML的形式---Jacob.jar (适用于Windows环境)
- powerDesigner生成excel版本的数据库文件
- 一个关于把集合拆分的有意思的小题目(将1到N的连续整数组成的集合划分成两个子集合)
- android view类 介绍