Map 是 Go 中的内置类型,它将键与值绑定到一起。可以通过键获取相应的值。
如何创建 map?makemake(map[KeyType]ValueType)map[KeyType]ValueType
personSalary := make(map[string]int)
personSalary
make
package main import ( "fmt" ) func main() { var personSalary map[string]int if personSalary == nil { fmt.Println("map is nil. Going to make one.") personSalary = make(map[string]int) } }
personSalarymakemap is nil. Going to make one.
向 map 中插入元素
personSalary
package main import ( "fmt" ) func main() { personSalary := make(map[string]int) personSalary["steve"] = 12000 personSalary["jamie"] = 15000 personSalary["mike"] = 9000 fmt.Println("personSalary map contents:", personSalary) }
personSalary map contents: map[steve:12000 jamie:15000 mike:9000]
也可以在声明时初始化一个数组:
package main import ( "fmt" ) func main() { personSalary := map[string]int { "steve": 12000, "jamie": 15000, } personSalary["mike"] = 9000 fmt.Println("personSalary map contents:", personSalary) }
personSalary
"mike"
personSalary map contents: map[steve:12000 jamie:15000 mike:9000]
string
访问 map 中的元素
map[key]
package main import ( "fmt" ) func main() { personSalary := map[string]int{ "steve": 12000, "jamie": 15000, } personSalary["mike"] = 9000 employee := "jamie" fmt.Println("Salary of", employee, "is", personSalary[employee]) }
jamieSalary of jamie is 15000
personSalary
package main import ( "fmt" ) func main() { personSalary := map[string]int{ "steve": 12000, "jamie": 15000, } personSalary["mike"] = 9000 employee := "jamie" fmt.Println("Salary of", employee, "is", personSalary[employee]) fmt.Println("Salary of joe is", personSalary["joe"]) }
上面的程序输出为:
Salary of jamie is 15000 Salary of joe is 0
joejoepersonSalary
我们如何检测一个键是否存在于一个 map 中呢?可以使用下面的语法:
value, ok := map[key]
okok
package main import ( "fmt" ) func main() { personSalary := make(map[string]int) personSalary["steve"] = 12000 personSalary["jamie"] = 15000 personSalary["mike"] = 9000 fmt.Println("personSalary map contents:", personSalary) value, ok := personSalary["Tom"] if ok == true { fmt.Println(value) } else { fmt.Println("Tom is not exist") } }
ok
Tom is not exist
range for 可用于遍历 map 中所有的元素(译者注:这里 range 操作符会返回 map 的键和值)。
package main import ( "fmt" ) func main() { personSalary := map[string]int{ "steve": 12000, "jamie": 15000, } personSalary["mike"] = 9000 fmt.Println("All items of a map") for key, value := range personSalary { fmt.Printf("personSalary[%s] = %d\n", key, value) } }
上面的程序输出如下:
All items of a map personSalary[mike] = 9000 personSalary[steve] = 12000 personSalary[jamie] = 15000
值得注意的是,因为 map 是无序的,因此对于程序的每次执行,不能保证使用 range for 遍历 map 的顺序总是一致的。
删除元素delete(map, key)delete
package main import ( "fmt" ) func main() { personSalary := map[string]int{ "steve": 12000, "jamie": 15000, } personSalary["mike"] = 9000 fmt.Println("map before deletion", personSalary) delete(personSalary, "steve") fmt.Println("map after deletion", personSalary) }
steve
map before deletion map[steve:12000 jamie:15000 mike:9000] map after deletion map[mike:9000 jamie:15000]map 的大小
用内置函数 len 获取 map 的大小:
package main import ( "fmt" ) func main() { personSalary := map[string]int{ "steve": 12000, "jamie": 15000, } personSalary["mike"] = 9000 fmt.Println("length is", len(personSalary)) }
len(personSalary)personSalarylength is 3
map 是引用类型
与切片一样,map 是引用类型。当一个 map 赋值给一个新的变量,它们都指向同一个内部数据结构。因此改变其中一个也会反映到另一个:
package main import ( "fmt" ) func main() { personSalary := map[string]int{ "steve": 12000, "jamie": 15000, } personSalary["mike"] = 9000 fmt.Println("Original person salary", personSalary) newPersonSalary := personSalary newPersonSalary["mike"] = 18000 fmt.Println("Person salary changed", personSalary) }
personSalarynewPersonSalarynewPersonSalarymike18000personSalarymike18000
Original person salary map[steve:12000 jamie:15000 mike:9000] Person salary changed map[jamie:15000 mike:18000 steve:12000]
将 map 作为参数传递给函数也是一样的。在函数中对 map 的任何修改都会影响在调用函数中看到。
package main import ( "fmt" ) func main() { personSalary := make(map[string]int) personSalary["steve"] = 12000 personSalary["jamie"] = 15000 personSalary["mike"] = 9000 fmt.Println("personSalary map contents:", personSalary) value, ok := personSalary["Tom"] if ok == true { fmt.Println(value) } else { fmt.Println("Tom is not exist") } for k, v := range personSalary { fmt.Println(k, v) } fmt.Println("Len: ", len(personSalary)) delete(personSalary, "mike") for k, v := range personSalary { fmt.Println(k, v) } fmt.Println("Len: ", len(personSalary)) newPersionSalary := personSalary newPersionSalary["tom"] = 5000 for k, v := range personSalary { fmt.Println(k, v) } fmt.Println("Len: ", len(newPersionSalary)) for k, v := range newPersionSalary { fmt.Println(k, v) } fmt.Println("Len: ", len(newPersionSalary)) }