- 固定长度,不能动静扭转
- 元素类型雷同
- 内存间断调配
// 申明了一个长度为 10 的整型数组,所有元素都被主动赋值为 0
var nums [10]int
// 或申明同时赋值
nums := [10]int{1, 2, 3, 4}
// 或自动识别数组长度
nums := [...]int{1,2,3,4,5,6,7,8,9,10}
// 或申明同时通过索引赋值
nums := [10]int{1:10, 3:10}
// 获取索引为 2 的值
nums[2]
// 批改索引为 2 的值
nums[2] = 10
a := [3]int{5, 78, 8}
var b [5]int
//not possible since [3]int and [5]int are distinct types
b = a
a := [3]int{5, 78, 8}
var b [3]int
// 胜利
b = a
a := [...]string{"USA", "China", "India", "Germany", "France"}
b := a
b[0] = "Singapore"
// a is [USA China India Germany France]
fmt.Println("a is", a)
// b is [Singapore China India Germany France]
fmt.Println("b is", b)
a := [...]float64{67.7, 89.8, 21, 78}
fmt.Println("length of a is:", len(a))
for i := 0; i < len(nums); i++ {fmt.Println("i:", i, ", j:", nums[i])
}
// value 是值传递,非援用传递
for key, value := range nums {fmt.Println("key:", key, ", value:", value)
}
// 应用下划线 _ 疏忽 key
for _, value := range nums {fmt.Println("value:", value)
}
// 应用下划线 _ 和等号 = 疏忽 key, value
for _, _ = range nums {}