实践出真理
package main
import (
"io"
"log"
"os"
)
func main() {
log.SetOutput(io.MultiWriter(os.Stdout))
log.SetFlags(log.Lshortfile)
// 固定数组长度
var fixedLnegA1 [5]int
fixedLnegA2 := [4]int{1, 2, 3}
log.Println("fixedLnegA1=", fixedLnegA1)
log.Println("fixedLnegA2=", fixedLnegA2)
// 动态数组,切片 slice, 声明时赋值
dynamicArray1 := []int{1, 2, 3, 4}
// 查看数组的数据类型
log.Printf("fixedLnegA1 %T\n", fixedLnegA1)
log.Printf("fixedLnegA2 %T\n", fixedLnegA2)
log.Printf("dynamicArray1 %T\n", dynamicArray1)
// 循环遍历数组
for _, vaule := range dynamicArray1 {
log.Println("value = ", vaule)
}
}
输出
main.go:17: fixedLnegA1= [0 0 0 0 0]
main.go:18: fixedLnegA2= [1 2 3 0]
main.go:24: fixedLnegA1 [5]int
main.go:25: fixedLnegA2 [4]int
main.go:26: dynamicArray1 []int
main.go:30: value = 1
main.go:30: value = 2
main.go:30: value = 3
main.go:30: value = 4