实践出真理

  1. package main
  2. import (
  3. "io"
  4. "log"
  5. "os"
  6. )
  7. func main() {
  8. log.SetOutput(io.MultiWriter(os.Stdout))
  9. log.SetFlags(log.Lshortfile)
  10. // 固定数组长度
  11. var fixedLnegA1 [5]int
  12. fixedLnegA2 := [4]int{1, 2, 3}
  13. log.Println("fixedLnegA1=", fixedLnegA1)
  14. log.Println("fixedLnegA2=", fixedLnegA2)
  15. // 动态数组,切片 slice, 声明时赋值
  16. dynamicArray1 := []int{1, 2, 3, 4}
  17. // 查看数组的数据类型
  18. log.Printf("fixedLnegA1 %T\n", fixedLnegA1)
  19. log.Printf("fixedLnegA2 %T\n", fixedLnegA2)
  20. log.Printf("dynamicArray1 %T\n", dynamicArray1)
  21. // 循环遍历数组
  22. for _, vaule := range dynamicArray1 {
  23. log.Println("value = ", vaule)
  24. }
  25. }

输出

  1. main.go:17: fixedLnegA1= [0 0 0 0 0]
  2. main.go:18: fixedLnegA2= [1 2 3 0]
  3. main.go:24: fixedLnegA1 [5]int
  4. main.go:25: fixedLnegA2 [4]int
  5. main.go:26: dynamicArray1 []int
  6. main.go:30: value = 1
  7. main.go:30: value = 2
  8. main.go:30: value = 3
  9. main.go:30: value = 4