The operation and use of map in Go (Add and remove Search)

Source: Internet
Author: User

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.