sync.RWMutex
var counter = struct{ sync.RWMutex m map[string]int }{m: make(map[string]int)}
从counter读取数据,使用读锁
counter.RLock() n := counter.m["some_key"] counter.RUnlock() fmt.Println("some_key:", n)
向counter写数据,使用写锁
counter.Lock() counter.m["some_key"]++ counter.Unlock()
参考:http://blog.golang.org/go-maps-in-action