我QQ:29295842 有知道告诉我下
GO 1.9 安全MAP 问题 不知道是不是这个 BUG 求解答为什么
```
package main
import (
"fmt"
"sync"
)
var (
map_list sync.Map //广告配置信息
wgx sync.WaitGroup //
)
func map_add(id string, rows_map map[string]interface{}) { //添加数据
map_list.Store(id, rows_map)
}
func map_revise(id string, rows_map map[string]interface{}) { //修改
wgx.Add(1) //线程数
go func() {
map_list.LoadOrStore(id, rows_map) //修改
wgx.Done()
}()
wgx.Wait() //等待
}
func map_delete(id string) { //删除
wgx.Add(1) //线程数
go func() {
map_list.Delete(id) //删除
wgx.Done()
}()
wgx.Wait() //等待
}
func map_read(id string) (bool, string, map[string]interface{}) { //读取
read_bool := false
value := make(map[string]interface{})
value2, err := map_list.Load(id) //key读取
if err {
if valuexa, ok := value2.(map[string]interface{}); ok {
read_bool = true
value = valuexa
}
}
//fmt.Print("===%v==%v==\n", data, err)
//遍历读取
map_list.Range(func(key, value2 interface{}) bool { //读取数据
fmt.Println(key, "-----------", value2)
// if valuexa, ok := value2.(map[string]interface{}); ok {
// read_bool = true
// //key = fmt.Sprintf("%v", key)
// value = valuexa
// }
return true
})
return read_bool, id, value
}
```
上边是我自己写的一个封装
rows_map := make(map[string]interface{})
rows_map["db_name"] = "098765"
rows_map["list"] = "1234567"
map_add("abc", rows_map)
rows_map2 := make(map[string]interface{})
rows_map2["db_name"] = "aaaaaa"
rows_map2["list"] = "bbbbbbb"
map_add("123", rows_map2)
rows_map3 := make(map[string]interface{})
rows_map3["db_name"] = "rrrrr"
rows_map3["list"] = "eeeeeee"
map_add("1234", rows_map3)
read_bool, key, re_map := map_read("abc")
fmt.Printf("==%v==%v==%v==\n", read_bool, key, re_map["db_name"])
如果代码这样写 我们读取到的值是
==true==abc==098765==
![a1.png](http://studygolang.qiniudn.com/170827/552dbf93932119b481298ac053f5c132.png)
//==================================
如果代码是这样的
rows_map := make(map[string]interface{})
rows_map["db_name"] = "098765"
rows_map["list"] = "1234567"
map_add("abc", rows_map)
rows_map["db_name"] = "aaaaaa"
rows_map["list"] = "bbbbbbb"
map_add("123", rows_map)
rows_map["db_name"] = "rrrrr"
rows_map["list"] = "eeeeeee"
map_add("1234", rows_map)
read_bool, key, re_map := map_read("abc")
fmt.Printf("==%v==%v==%v==\n", read_bool, key, re_map["db_name"])
为什么写入的数据都一样呢不知道那位能回答下
![a2.png](http://studygolang.qiniudn.com/170827/4a3d5c78c4255231a31603ce7872416f.png)
```
// 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
更多评论
这个问题和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