This is a created article in which the information may have evolved or changed.
Code from "Go Language development tutorial"
Thread Sync: Sync. Mutexpackage mainimport ("Errors" "FMT" "Sync") type MyMap struct {MP Map[string]intmutex *sync. Mutex}func (This *mymap) Get (key string) (int, error) {This.mutex.Lock () I, OK: = This.mp[key]this.mutex.unlock () if!ok {re Turn I, errors. New ("not exist")}return I, Nil}func (the *mymap) Set (key string, V int) {This.mutex.Lock () defer this.mutex.Unlock () this. Mp[key] = V}func SetValue (M *mymap) {var a runea = ' a ' for I: = 0; i <; i++ {M.set (String (A+rune (i)), i)}}func (this *mymap) Display () {this.mutex.Lock () defer this.mutex.Unlock () for k, V: = Range This.mp {fmt. Println (k, "=", V)}}func main () {m: = &mymap{mp:make (Map[string]int), mutex:new (sync. Mutex)}go SetValue (M) go M.display () var str stringfmt. Scan (&STR)}