数组
数组初始化
var arr [4]int
arr[0] = 1
arr[1] = 2
fmt.Println(arr)
var a = [4]int{1, 2, 3, 4}
fmt.Println(a)
var b = [4]int{1, 2, 3}
fmt.Println(b)
var c [4]int
fmt.Println(c)
//对指定下标位置赋值,从零开始
var d = [4]int{2: 1}
fmt.Println(d)
f := [...]int{1, 3, 4, 2, 4}
fmt.Println(f)
输出结果:
[1 2 0 0]
[1 2 3 4]
[1 2 3 0]
[0 0 0 0]
[0 0 1 0]
[1 3 4 2 4]
for循环遍历数组
f := [...]int{1, 3, 4, 2, 4}
for index, val := range f {
println(index, val)
}
数组是值类型
f := [...]int{1, 2, 3, 4, 5}
f2 := f
f2[1] = 500
fmt.Println(f)
fmt.Println(f2)
输出结果
[1 2 3 4 5]
[1 500 3 4 5]
多维数组
arr3 := [3][3]int{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}
切片
slice,就是动态数组
初始化
var s1 []int
var s2 []int
s1 = append(s1, 1, 2, 3)
s2 = append(s2, 4, 5, 6)
s1 = append(s1, s2...)
s3 := make([]int, 10, 64)
注意,append数据引起扩容的时候,引用地址会发生改变,所以需要重新赋值,扩容是按2倍原来长度扩容的
标签:06,int,fmt,var,切片,Println,数组,Go,s1 From: https://www.cnblogs.com/huacha/p/16807753.html