实例
package main
import (
"fmt"
)
func main() {
arr := make([]int, 0)
//arr := make([]int) is wrong
//arr := make([]int, 100) is right
arr = append(arr, 1)
arr = append(arr, 2)
arr = append(arr, 3)
arr = append(arr, 4)
mymap := make(map[int]string)
//mymap := make(map[int]string, 0)
//mymap := make(map[int]string,1) is right
mymap[0] = "10"
mymap[1] = "110"
mymap[2] = "1110"
for _, val := range arr {
fmt.Println("val:", val)
}
for _, val := range mymap {
fmt.Println("val:", val)
}
fmt.Println()
}
运行结果
val: 1
val: 2
val: 3
val: 4
val: 10
val: 110
val: 1110
有疑问加站长微信联系(非本文作者)