Code_014_map_operator Project Main.gopackage mainimport ("FMT") func Deletemap (M map[int]string, key int) {Delete (M, K EY) for k, V: = range m {fmt. Printf ("Len (m) =%d,%d---->%s\n", Len (m), K, V)}}func main () {//assignment--Modify and append m1: = map[int]string{1: "Ck_god", 2: " God_girl "} m1[1] =" xxx "m1[3] =" Lily "//Append, go bottom automatically allocates space fmt for map. PRINTLN (m1) M2: = Make (map[int]string, ten) m2[0] = "AAA" m2[1] = "BBB" FMT. Println (m2) fmt. Println (M2[0], m2[1]) fmt. Println ("")//traverse m3: = map[int]string{1: "Ck_god", 2: "God_girl"} for K, V: = range m3 {fmt. Printf ("%d--->>>%s\n", K, V)} for k: = Range m3 {fmt. Printf ("%d--->%s\n", K, M3[k])} FMT. Println ("")//Get the value of map, OK: = M3[1] FMT. Println ("value =", Value, ", OK =", OK) value2, ok2: = M3[3] FMT. Println ("value2 =", value2, ", Ok2 =", Ok2) fmt. Println ("")//delete M4: = map[int]string{1: "Ck_god", 2: "God_girl"} Delete (M4, 2)//delete the map for K with a key value of 2, V: = Range M4 {FMT. Printf ("%d--->%s\n", K, V)} FMT. Println ("")//map passed as a parameter//in the letterThe pass-through mapping does not produce a copy of the map, not a value pass, but a reference pass: M: = map[int]string{1: "Ck_god", 2: "God_girl", 3: "Girl_angle"} Deletemap (M, 3) for K, V: = range m {fmt. Printf ("Len (m) =%d,%d---->%s\n", Len (m), K, V)}}
The results of the implementation are as follows:
map[1:xxx 2:god_girl 3:lily]map[1:bbb 0:aaa]aaa bbb1--->>>ck_god2--->>>god_girl2--->god_girl1--->ck_godvalue = ck_god , ok = truevalue2 = , ok2 = false1--->ck_godlen(m)=2, 1 ----> ck_godlen(m)=2, 2 ----> god_girllen(m)=2, 1 ----> ck_godlen(m)=2, 2 ----> god_girl
The operation and use of map in Go (Add and remove Search)