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 mainimport "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 mainimport "fmt" func main () { m := map [string ]map [string ]string { "member" : { "name" : "larry" , "company" : "PolarSnow Inc." , "website" : "https://lvrui.io" , }, } fmt.Println(m["member" ]["name" ]) }
执行结果:
其他声明map的方式 1 2 3 4 5 6 7 8 9 package mainimport "fmt" func main () { m := make (map [string ]string ) var mm map [string ]string fmt.Println(m, mm) }
执行结果:
map的遍历 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 package mainimport "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 mainimport "fmt" func main () { m := map [string ]string { "name" : "larry" , "company" : "PolarSnow Inc." , "website" : "https://lvrui.io" , } fmt.Println(m["website" ]) fmt.Println(m["site" ]) 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 mainimport "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 mainimport "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 mainimport "fmt" func main () { m := map [string ]string { "name" : "larry" , "company" : "PolarSnow Inc." , "website" : "https://lvrui.io" , } fmt.Println(len (m)) }
执行结果:
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 mainimport "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]