1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
package main
import (
"fmt"
)
// 元素类型为map的切片:
func main() {
var mapSlice = make([]map[string]string, 3)
for k, v := range mapSlice {
fmt.Printf("key: %v, value: %v \n", k, v)
} // 定义并初始化,一个切片,其每个元素都是一个map类型
// 初始化切片的第一个元素,即一个map,用make分配内存
mapSlice[0] = make(map[string]string, 3)
// 向其中填充值
mapSlice[0]["name"] = "wang"
mapSlice[0]["age"] = "18"
mapSlice[0]["passwd"] = "123"
mapSlice[1] = make(map[string]string, 2)
mapSlice[1]["name"] = "wang"
mapSlice[1]["shuai"] = "haha"
for k, v := range mapSlice {
fmt.Printf("key: %v, value: %v \n", k, v)
}
}
ten@LAPTOP-5TPRJ6VQ MINGW64 /d/workstation/mycode/gocode/src/learngo/basic_grammar/07map (master)
$ go run main.go
key: 0, value: map[]
key: 1, value: map[]
key: 2, value: map[]
key: 0, value: map[age:18 name:wang passwd:123]
key: 1, value: map[name:wang shuai:haha]
key: 2, value: map[]
|