1, the map
A map is a dictionary that queries the semantics of a word. It’s a hash table that has a key and a value, and you can access the value through the key.
The value of a map can be any data type. A map, like a slice, is a reference type.
1.1 the grammar of the map
Var country=map[string] string{"China":" Beijing ", "Japan":" Tokyo ", "France":" Paris ", "Italy":" Rome" } for k,v:=range country{ fmt.Printf("key:%s\t,value:%s\n",k,v) } if value,ok:=country["USA"]; Ok {fmt.Println(" capital ",value)}else{fmt.Println(" not found USA!") Delete (country,"Japan") for k,v:=range country{fmt.Printf("%s\t,%s\n",k,v)}}Copy the code
1.2 Map is a reference type
Maps are similar to slices in that they are reference types. When a map is assigned to a new variable, they refer to the same block of memory. Therefore, changing the contents of both variables can cause the data they point to to change.
Sample, verify that map is a reference type
package main import "fmt" func main() { personMoney:=map[string] int{ "JackMa":9999999999, "PonyMa(":9999999998, Println(" how much money ",personMoney) newP:=personMoney newP["JackMa"]=100 FMT.Println(" how much money ",personMoney) ,personMoney)} /* How much money map[JackMa: 999999999999 PonyMa(:9999999998 StevenDing:88888888] now? map[JackMa:100 PonyMa(:9999999998 StevenDing:88888888] */Copy the code