Go Language---Map

Source: Internet
Author: User

Go language---map78890185. Map usage:
Type persondb struct {Id string Name string Address string}fmt. Println ("=========map===========")//map is a bunch of unordered sets of key-value pairs that are built-in in Golang and can be used directly, unlike Java needs to introduce hashmap var personmap map[ STRING]PERSONDB//declares a map variable, string is the type of the key, PERSONDB is the type of the value Personmap = Make (Map[string]perso NDB, 10)//create a map with initial capacity of personmap["1"] = Persondb{id: "1", Name: "Zs", Address: "Fangshan District, Beijing"}// Assign a value to the map element personmap["2"] = Persondb{id: "2", Name: "ls", Address: "Beijing Haidian District"}//Use the built-in function Delete to delete the elements of key= "1" in Personmap, if not To the key, then nothing will happen, if Personmap is nil, then the wrong delete (Personmap, "1") if value, OK: = personmap["2"]; OK {//Find the key=2 element, then Ok=true,value is the value found; otherwise ok=false FMT. Println (Value)} PERSONMAP2: = map[string]persondb{"3": persondb{"3", "ww", "Hangzhou City West L. District"}}//Create and initialize a map personmap2[" 4 "] = persondb{" 4 "," ll "," Dongcheng District of Beijing "} for K, V: = range PERSONMAP2 {//Use range to traverse the elements of the map in FMT. Println (k, v) if k = = "3" {v.name = "WwUPdate "personmap2[" 3 "] = V//Can change value//The following method of assignment will be an error, only value can be assigned, cannot directly change its member variable//PERSONMAP2 ["3"]. Name = "Wwupdate"//cannot assign to struct field personmap2["3"]. Name in Map}} FMT.     Println (PERSONMAP2)

Output Result:
Fatal Error:concurrent Map writes
The result is that the map is not thread-safe.
As to why it is not thread-safe, analyze it later

Three. Map pass value or pass reference?

Run the following code:

//map传值还是传引用? map和slice,channel一样,内部都有一个指向真正存储空间的指针,所以,即使传参时是对值的复制(传值),但都指向同一块存储空间。    var myMapV map[string]int    var myMap1 map[string]int    myMapV = map[string]int{"value": 0}    myMap1 = myMapV    fmt.Println("m =", myMapV)    fmt.Println("m1 =", myMap1)    myMap1["value"] = 1    fmt.Println("m =", myMapV)    fmt.Println("m1 =", myMap1)
Two. is the map thread-safe?

Run the following code:

//map不是线程安全的。     cMap := make(map[string]int)    for i := 0; i < 100; i++ {        go func() {            for j := 0; j < 100; j++ {            cMap[fmt.Sprintf("%d", j)] = j //fatal error: concurrent map writes            }        }()    }     time.Sleep(3 * time.Second)

Output Result:
m = map[value:0]
M1 = map[value:0]
m = map[value:1]
M1 = Map[value:1]
We found that when modifying the m1,m also changed, it seems to be a reference, but in fact map is also transmitted value, its principle and array of slices similar. The map maintains a pointer that points to the true map storage space. We can describe the map as the following structure:

Type Map[key]value struct{
Impl *map_k_v
}
Type Map_k_v struct{
//......
}
In fact, like map and Slice,channel, there is a pointer to the real storage space inside, so even if the parameter is a copy of the value (value), it all points to the same piece of storage space.

Go Language---Map

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.