[关闭]
@phper 2018-03-13T10:11:37.000000Z 字数 4475 阅读 2507

13.Map

Golang

原文:https://golangbot.com/maps/


欢迎来到Golang 系列教程第 13 章。

什么是 map ?

map 是在 Go 中将值(value)与键(key)关联的内置类型。通过相应的键可以获取到值。

如何创建 map ?

通过向 make 函数传入键和值的类型,可以创建 map。make(map[type of key]type of value) 是创建 map 的语法。

  1. personSalary := make(map[string]int)

上面的代码创建了一个名为 personSalary 的 map,其中键是 string 类型,而值是 int 类型。

map 的零值是 nil。如果你想添加元素到 nil map 中,会触发运行时 panic。因此 map 必须使用 make 函数初始化。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. var personSalary map[string]int
  7. if personSalary == nil {
  8. fmt.Println("map is nil. Going to make one.")
  9. personSalary = make(map[string]int)
  10. }
  11. }

在线运行程序

上面的程序中,personSalary 是 nil,因此需要使用 make 方法初始化,程序将输出 map is nil. Going to make one.

给 map 添加元素

给 map 添加新元素的语法和数组相同。下面的程序给 personSalary map 添加了几个新元素。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. personSalary := make(map[string]int)
  7. personSalary["steve"] = 12000
  8. personSalary["jamie"] = 15000
  9. personSalary["mike"] = 9000
  10. fmt.Println("personSalary map contents:", personSalary)
  11. }

在线运行程序

上面的程序输出:personSalary map contents: map[steve:12000 jamie:15000 mike:9000]

你也可以在声明的时候初始化 map。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. personSalary := map[string]int {
  7. "steve": 12000,
  8. "jamie": 15000,
  9. }
  10. personSalary["mike"] = 9000
  11. fmt.Println("personSalary map contents:", personSalary)
  12. }

在线运行程序

上面的程序声明了 personSalary,并在声明的同时添加两个元素。之后又添加了键 mike。程序输出:

  1. personSalary map contents: map[steve:12000 jamie:15000 mike:9000]

键不一定只能是 string 类型。所有可比较的类型,如 boolean,interger,float,complex,string 等,都可以作为键。关于可比较的类型,如果你想了解更多,请访问 http://golang.org/ref/spec#Comparison_operators

获取 map 中的元素

目前我们已经给 map 添加了几个元素,现在学习下如何获取它们。获取 map 元素的语法是 map[key]

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. personSalary := map[string]int{
  7. "steve": 12000,
  8. "jamie": 15000,
  9. }
  10. personSalary["mike"] = 9000
  11. employee := "jamie"
  12. fmt.Println("Salary of", employee, "is", personSalary[employee])
  13. }

在线运行程序

上面的程序很简单。获取并打印员工 jamie 的薪资。程序输出 Salary of jamie is 15000

如果获取一个不存在的元素,会发生什么呢?map 会返回该元素类型的零值。在 personSalary 这个 map 里,如果我们获取一个不存在的元素,会返回 int 类型的零值 0

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. personSalary := map[string]int{
  7. "steve": 12000,
  8. "jamie": 15000,
  9. }
  10. personSalary["mike"] = 9000
  11. employee := "jamie"
  12. fmt.Println("Salary of", employee, "is", personSalary[employee])
  13. fmt.Println("Salary of joe is", personSalary["joe"])
  14. }

在线运行程序

上面程序输出:

  1. Salary of jamie is 15000
  2. Salary of joe is 0

上面程序返回 joe 的薪资是 0。personSalary 中不包含 joe 的情况下我们不会获取到任何运行时错误。

如果我们想知道 map 中到底是不是存在这个 key,该怎么做:

  1. value, ok := map[key]

上面就是获取 map 中某个 key 是否存在的语法。如果 ok 是 true,表示 key 存在,key 对应的值就是 value ,反之表示 key 不存在。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. personSalary := map[string]int{
  7. "steve": 12000,
  8. "jamie": 15000,
  9. }
  10. personSalary["mike"] = 9000
  11. newEmp := "joe"
  12. value, ok := personSalary[newEmp]
  13. if ok == true {
  14. fmt.Println("Salary of", newEmp, "is", value)
  15. } else {
  16. fmt.Println(newEmp,"not found")
  17. }
  18. }

在线运行程序

上面的程序中,第 15 行,joe 不存在,所以 ok 是 false。程序将输出:

  1. joe not found

遍历 map 中所有的元素需要用 for range 循环。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. personSalary := map[string]int{
  7. "steve": 12000,
  8. "jamie": 15000,
  9. }
  10. personSalary["mike"] = 9000
  11. fmt.Println("All items of a map")
  12. for key, value := range personSalary {
  13. fmt.Printf("personSalary[%s] = %d\n", key, value)
  14. }
  15. }

在线运行程序

上面程序输出:

  1. All items of a map
  2. personSalary[mike] = 9000
  3. personSalary[steve] = 12000
  4. personSalary[jamie] = 15000

有一点很重要,当使用 for range 遍历 map 时,不保证每次执行程序获取的元素顺序相同。

删除 map 中的元素

删除 mapkey 的语法是 delete(map, key)。这个函数没有返回值。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. personSalary := map[string]int{
  7. "steve": 12000,
  8. "jamie": 15000,
  9. }
  10. personSalary["mike"] = 9000
  11. fmt.Println("map before deletion", personSalary)
  12. delete(personSalary, "steve")
  13. fmt.Println("map after deletion", personSalary)
  14. }

在线运行程序

上述程序删除了键 "steve",输出:

  1. map before deletion map[steve:12000 jamie:15000 mike:9000]
  2. map after deletion map[mike:9000 jamie:15000]

获取 map 的长度

获取 map 的长度使用 len 函数。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. personSalary := map[string]int{
  7. "steve": 12000,
  8. "jamie": 15000,
  9. }
  10. personSalary["mike"] = 9000
  11. fmt.Println("length is", len(personSalary))
  12. }

在线运行程序

上述程序中的 len(personSalary) 函数获取了 map 的长度。程序输出 length is 3

Map 是引用类型

slices 类似,map 也是引用类型。当 map 被赋值为一个新变量的时候,它们指向同一个内部数据结构。因此,改变其中一个变量,就会影响到另一变量。

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func main() {
  6. personSalary := map[string]int{
  7. "steve": 12000,
  8. "jamie": 15000,
  9. }
  10. personSalary["mike"] = 9000
  11. fmt.Println("Original person salary", personSalary)
  12. newPersonSalary := personSalary
  13. newPersonSalary["mike"] = 18000
  14. fmt.Println("Person salary changed", personSalary)
  15. }

在线运行程序

上面程序中的第 14 行,personSalary 被赋值给 newPersonSalary。下一行 ,newPersonSalarymike 的薪资变成了 18000personSalaryMike 的薪资也会变成 18000。程序输出:

  1. Original person salary map[steve:12000 jamie:15000 mike:9000]
  2. Person salary changed map[steve:12000 jamie:15000 mike:18000]

当 map 作为函数参数传递时也会发生同样的情况。函数中对 map 的任何修改,对于外部的调用都是可见的。

Map 的相等性

map 之间不能使用 == 操作符判断,== 只能用来检查 map 是否为 nil

  1. package main
  2. func main() {
  3. map1 := map[string]int{
  4. "one": 1,
  5. "two": 2,
  6. }
  7. map2 := map1
  8. if map1 == map2 {
  9. }
  10. }

在线运行程序

上面程序抛出编译错误 invalid operation: map1 == map2 (map can only be compared to nil)

判断两个 map 是否相等的方法是遍历比较两个 map 中的每个元素。我建议你写一段这样的程序实现这个功能 :)。

我在一个程序里实现了我们讨论过的所有概念。你可以从 github 下载代码。

这就是 map 。谢谢你的阅读。祝好。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注