数组
数组是一段固定长度的连续内存区域。在Go语言中数组从声明时就确定,使用时可以需改数组成员,但不可以修改数组大小。
数组声明及初始化
声明
var 数组变量名 [元素数量]T // T是任意基本类型
初始化
var team = [3]string
team[0] = "Tom"
team[1] = "Jerry"
team[2] = "Marry"
fmt.Println(team)
var books = [3]string{"Go语言实践", "JAVA项目实践", "javascript设计模式"}
var students = [...]string{"张三","李四","王五","陈七"} // "..."标识让编译器确定数组的大小,此处为4
遍历数组
var team = [3]string
team[0] = "Tom"
team[1] = "Jerry"
team[2] = "Marry"
for k, v := range team {
fmt.Println(k, v)
}
//结果:
//0 Tom
//1 Jerry
//2 Marry