package main
import (
"fmt"
)
func main() {
var mapSlice []map[string]int
fmt.Printf("before mapSlice init %v len=%d,cap%d\n",mapSlice,len(mapSlice),cap(mapSlice))
mapSlice = make([]map[string]int, 5, )
fmt.Printf("after mapSlice init %v len=%d,cap%d\n",mapSlice,len(mapSlice),cap(mapSlice))
for index, value := range mapSlice {
fmt.Printf("index:%d value:%v\n", index, value)
}
fmt.Println()
mapSlice[0] = make(map[string]int, 5)
fmt.Println("01 before mapSlice[0] init",len(mapSlice[0]),cap(mapSlice),mapSlice[0])
fmt.Printf("01 before mapSlice[0]地址=%p\n\n",&mapSlice[0])
mapSlice[0]["a"] = 100
mapSlice[0]["b"] = 200
mapSlice[0]["c"] = 300
mapSlice[0]["d"] = 400
mapSlice[0]["e"] = 500
fmt.Println("02 before mapSlice[0] init",len(mapSlice[0]),cap(mapSlice),mapSlice[0])
fmt.Printf("02 before mapSlice[0]地址=%p\n\n",&mapSlice[0])
mapSlice[0]["f"] = 600
fmt.Println("03 before mapSlice[0] init",len(mapSlice[0]),cap(mapSlice),mapSlice[0])
fmt.Printf("03 before mapSlice[0]地址=%p\n\n",&mapSlice[0])
fmt.Println("04 after mapSlice[0] init",len(mapSlice[0]),cap(mapSlice))
for index, value := range mapSlice {
fmt.Printf("index:%d value:%v\n", index, value)
}
}
输出:
before mapSlice init [] len=0,cap0
after mapSlice init [map[] map[] map[] map[] map[]] len=5,cap5
index:0 value:map[]
index:1 value:map[]
index:2 value:map[]
index:3 value:map[]
index:4 value:map[]
01 before mapSlice[0] init 0 5 map[]
01 before mapSlice[0]地址=0xc0000b6330
02 before mapSlice[0] init 5 5 map[a:100 b:200 c:300 d:400 e:500]
02 before mapSlice[0]地址=0xc0000b6330
03 before mapSlice[0] init 6 5 map[a:100 b:200 c:300 d:400 e:500 f:600]
03 before mapSlice[0]地址=0xc0000b6330
04 after mapSlice[0] init 6 5
index:0 value:map[a:100 b:200 c:300 d:400 e:500 f:600]
index:1 value:map[]
index:2 value:map[]
index:3 value:map[]
index:4 value:map[]