GOLANG学习18. 数组声明
package main
import (
"fmt"
)
func testArray() {
//定义长度为5的int型数组
var a1 [5]int = [5]int{1, 2, 3, 4, 5}
var a2 = [5]int{1, 2, 3, 4, 5}
var a3 = […]int{22, 44, 56, 77, 8, 78, 33}
//下标对应的值,没有的补0
var a4 = […]int{1: 100, 4: 329}
//下标对应的字符串,没有补空
var a5 = […]string{1: "hello", 3: "world"}
//二维数组
var a6 [2][5]int = […][5]int{{1, 2, 3, 4, 5}, {22, 23, 24, 25, 26}}
fmt.Println(a1)
fmt.Println(a2)
fmt.Println(a3)
fmt.Println(a4)
fmt.Println(a5)
//遍历二维数组
for k1, v1 := range a6 {
for k2, v2 := range v1 {
fmt.Printf("(%d,%d)=%d\n", k1, k2, v2)
}
}
}
func main() {
testArray()
}