我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)
有疑问加站长微信联系(非本文作者)
![](https://static.golangjob.cn/static/img/footer.png?imageView2/2/w/280)