Go基础之Map

map中的key

Go语言中对map中的key的数据类型有如下要求:

  • map使用哈希表, key必须可以比较相等
  • 除了slice, map, function的内建类型都可以作为key
  • struct类型如果不包含第二条中排除的数据类型, 也可以做为key

map的声明与赋值

1
2
3
4
5
6
7
8
9
10
11
12
package main

import "fmt"

func main() {
m := map[string]string {
"name": "larry",
"company": "PolarSnow Inc.",
"website": "https://lvrui.io",
}
fmt.Println(m)
}

执行结果:

1
map[name:larry company:PolarSnow Inc. website:https://lvrui.io]

嵌套Map

1
2
3
4
5
6
7
8
9
10
11
12
13
14
package main

import "fmt"

func main() {
m := map[string]map[string]string {
"member": {
"name": "larry",
"company": "PolarSnow Inc.",
"website": "https://lvrui.io",
},
}
fmt.Println(m["member"]["name"])
}

执行结果:

1
larry

其他声明map的方式

1
2
3
4
5
6
7
8
9
package main

import "fmt"

func main() {
m := make(map[string]string) // m == empty map
var mm map[string]string // mm == nil
fmt.Println(m, mm)
}

执行结果:

1
map[] map[]

map的遍历

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package main

import "fmt"

func main() {
m := map[string]string {
"name": "larry",
"company": "PolarSnow Inc.",
"website": "https://lvrui.io",
}

for k, v := range m {
fmt.Println(k, v)
}
}

执行结果:

1
2
3
company PolarSnow Inc.
website https://lvrui.io
name larry

注意: map是无序的

读取map中的值

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import "fmt"

func main() {
m := map[string]string {
"name": "larry",
"company": "PolarSnow Inc.",
"website": "https://lvrui.io",
}

fmt.Println(m["website"])

fmt.Println(m["site"]) // map中无此key, 但是不会报错, 拿到的是string的zero value

// 通常判断map取值有没有成功的方式
if address, ok := m["website"]; ok {
fmt.Println(address)
} else {
fmt.Println("Key does not exist")
}

}

执行结果:

1
2
3
https://lvrui.io

https://lvrui.io

增加/修改元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

import "fmt"

func main() {
m := map[string]string {
"name": "larry",
"company": "PolarSnow Inc.",
"website": "https://lvrui.io",
}

fmt.Println(m)

m["country"] = "China"

fmt.Println(m)

m["name"] = "Polar Snow"

fmt.Println(m)

}

执行结果:

1
2
3
map[company:PolarSnow Inc. website:https://lvrui.io name:larry]
map[name:larry company:PolarSnow Inc. website:https://lvrui.io country:China]
map[name:Polar Snow company:PolarSnow Inc. website:https://lvrui.io country:China]

Go对map添加/修改元素的操作形式, 与Python相同

删除元素

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package main

import "fmt"

func main() {
m := map[string]string {
"name": "larry",
"company": "PolarSnow Inc.",
"website": "https://lvrui.io",
}

fmt.Println(m)

delete(m, "company")

fmt.Println(m)

}

执行结果:

1
2
map[name:larry company:PolarSnow Inc. website:https://lvrui.io]
map[name:larry website:https://lvrui.io]

获取map元素的个数

1
2
3
4
5
6
7
8
9
10
11
12
13
package main

import "fmt"

func main() {
m := map[string]string {
"name": "larry",
"company": "PolarSnow Inc.",
"website": "https://lvrui.io",
}

fmt.Println(len(m))
}

执行结果:

1
3

map的应用

取出所有key和所有value

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package main

import "fmt"

func main() {
m := map[string]string {
"name": "larry",
"company": "PolarSnow Inc.",
"website": "https://lvrui.io",
}

var key []string
var value []string

for k, v := range m {
key = append(key, k)
value = append(value, v)
}

fmt.Println(key)
fmt.Println(value)

}

执行结果:

1
2
[name company website]
[larry PolarSnow Inc. https://lvrui.io]