GO 1.9 安全MAP 问题 不知道是不是这个 BUG 求解答为什么

long · · 3094 次点击
这个问题和sync.Map没关系,要理解go本身的builtin map和slice一样都是很简单的描述性struct类型,里面存的是指向 **存实际内容的数据结构** 的指针 **Like slices, maps hold references to an underlying data structure. If you pass a map to a function that changes the contents of the map, the changes will be visible in the caller.** https://golang.org/doc/effective_go.html#maps
#2
更多评论
``` // Store sets the value for a key. func (m *Map) Store(key, value interface{}) { read, _ := m.read.Load().(readOnly) if e, ok := read.m[key]; ok && e.tryStore(&value) { return } m.mu.Lock() read, _ = m.read.Load().(readOnly) if e, ok := read.m[key]; ok { if e.unexpungeLocked() { // The entry was previously expunged, which implies that there is a // non-nil dirty map and this entry is not in it. m.dirty[key] = e } e.storeLocked(&value) } else if e, ok := m.dirty[key]; ok { e.storeLocked(&value) } else { if !read.amended { // We're adding the first new key to the dirty map. // Make sure it is allocated and mark the read-only map as incomplete. m.dirtyLocked() m.read.Store(readOnly{m: read.m, amended: true}) } m.dirty[key] = newEntry(value) } m.mu.Unlock() } ``` store 存的是引用,想要避免可以这样 ``` func map_add(id string, rows_map map[string]interface{}) { row := rows_map map_list.Store(id, row) } ```
#1