The structure of the map

The reference type starts with nil, and an empty slice can be used, but an empty map cannot

Grammatical structure:

1. Create a map

Var map1 map[key]value nil map, Var map2=make(map[key type])value type Var map3=map[key type]value type {key:value,key:value.... }Copy the code
Package main import "FMT" func main() {/* map: map is A collection of key/value pairs. Unordered key-value pairs are stored. B. Keys cannot be duplicated, and keys cannot be duplicated in a map that corresponds to a value. If repeated, the new value overwrites the old one. Grammatical structure: Var map1 map[int]string var map2=make(map[int]string) var map3=map[string]int{"Go":98,"Python":87,"java":79,"Html":93} fmt.Println(map1) fmt.Println(map2) fmt.Println(map3) fmt.Println(map1==nil) fmt.Println(map2==nil) fmt.Println(map3 ==nil) if map1==nil { map1=make(map[int]string) fmt.Println(map1==nil) } }Copy the code

The use of the map

Package main import "FMT" func main() {/* map: map is A collection of key/value pairs. Unordered key-value pairs are stored. B. Keys cannot be duplicated, and keys cannot be duplicated in a map that corresponds to a value. If repeated, the new value overwrites the old one. Grammatical structure: Var map1 map[key]value nil map, Var map2=make(map[key type])value type Var map3=map[key type]value type {key:value,key:value.... Var map1 map[int]string var map2=make(map[int]string) var map3=map[string]int{"Go":98,"Python":87,"java":79,"Html":93} fmt.Println(map1) fmt.Println(map2) fmt.Println(map3) Fmt. Println(map1==nil) fmt.Println(map2==nil) fmt.Println(map3 ==nil) //2.nil map cannot use if map1==nil { map1=make(map[int]string) fmt.Println(map1==nil) } //3. Storage keys to map map1 [1] = "hello" map1 [2] = "world" map1 [3] = "mm" map1 [4] = "fasd map1" [5] = "FFFF" / / 4. Println(map1) fmt.println (map1[4]) fmt.println (map1[40]) // Returns two values, v1 is data, Map1 [40] if OK {fmt.Println(" corresponding value: ",v1)}else {fmt.Println("key does not exist ")} //5 Println(map1) map1[3]=" Println(map1) //6" Delete (map1,3) fmt.println (map1) delete(map1,30) fmt.println (map1) //7.map length fmt.println (len(map1))}Copy the code