See comments in the code:
Main
(
"FMT"
"Sync"
)
Main () {
map[string]int{"a": 1 , "B": 2, "C": 3, "D": 4, "E": 5, "F": 6}
FMT. Println (m)
FMT. Println ("-------")
WG: = &sync. waitgroup{}
WG. ADD (3)
go funcmap[string]int] {
WG. Done ()
p["a"] = Ten
} (m)// because the map in the Go language is a reference type, although the Go language function is called by value, that is, the function holds a copy of the parameter, but because it is a reference type, it still points to the map m, a pointer like C.
go funcmap[String]int] {
WG. Done ()
p["a"] = one
} (m)
go funcmap[string]int) {
WG. Done ()
p["a"] =
(m)
WG. Wait ()
FMT. Println (M)
/*
Because the map is a reference type, even if the function is called, the parameter copy still points to the mapping m, so 3 goroutine concurrently write the same mapping m, and the classmates who write too many threads know that for shared variables, resources,
Concurrent read and write will produce competition, so the shared resources are destroyed, so either lock, or use channel queuing serialization, in short, to exclusive access.
So remember: Many goroutine read and write the same mapping, to protect yo, locking can also, using channel serialization processing can also ...
*/
}
Note: This article is only my personal notes, if there are errors and omissions, please correct me, study together, my e-mail: htyu_0203_39@sina.com