1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | package main import ( "fmt" ) func main() { //创建 at:=[6]int{} //下标赋值 ap:=[5]int{3:2}//下标3的值赋值为2 fmt.Println("ap[3]:", ap[3]) //遍历1 as := [3]int{1, 2, 3} for i, p := range as { fmt.Println(i, "---", p) at[i] = as[i] fmt.Println(i, "---", at[i]) } //遍历2 le:=len(as) for i:=0;i<le;i++{ fmt.Println(as[i]) } //指针数组 av := [3]int{1, 2, 3} var pa [3]*int for i, p := range av { fmt.Println(i, "---", p) pa[i] = &av[i] //pa[i]为地址 fmt.Println(i, "---", *(pa[i])) fmt.Println(i, "---", pa[i]) } //数组指针 var pa2 *[3]int pa2 = &as fmt.Println(*pa2) fmt.Println(cap(*pa2)) } |
结果
ap[3]: 2
0 --- 1
0 --- 1
1 --- 2
1 --- 2
2 --- 3
2 --- 3
1
2
3
0 --- 1
0 --- 1
0 --- 0xc00011e160
1 --- 2
1 --- 2
1 --- 0xc00011e168
2 --- 3
2 --- 3
2 --- 0xc00011e170
[1 2 3]
3