mapスレッドが安全でない同時読み書きの問題はsyncを使用しないほうがよい.map、システムライブラリに問題があります
システムはsyncを提供した.mapは使わないほうがいいです.システムにスレッドが付いている安全なmapにはバグがあります.
map+スレッドロックまたは読み書きロックを選択して状況を見る
sync.mapのバグ..
-----------------------------------
{{0 0} {{map[] true}} map[c:0xc000006038 a:0xc000006028 b:0xc000006030] 0} 2 [123 125]
{mu:{state:0 sema:0} read:{v:{m:map[] amended:true}} dirty:map[c:0xc00007a028 a:0xc00007a018 b:0xc00007a020] misses:0} 2 [123 125] ----0 map[a:b b:c c:c] {"a":"b","b":"c","c":"c"}
これはjsonに相当する.Marshal()はsyncに対して標準mapのみをサポートする.Mapは再処理しなければなりません
map+スレッドロックまたは読み書きロックを選択して状況を見る
sync.mapのバグ..
tmp:=sync.Map{}
tmp.Store("a","b")
tmp.Store("b","c")
tmp.Store("c","c")
fmt.Println(tmp)
msg,_:= json.Marshal(tmp)
fmt.Println(len(msg))
fmt.Println(msg)
-----------------------------------
{{0 0} {{map[] true}} map[c:0xc000006038 a:0xc000006028 b:0xc000006030] 0} 2 [123 125]
package main
import (
"encoding/json"
"fmt"
"sync"
)
func main() {
tmp := sync.Map{}
pp := make(map[string]string)
tmp.Store("a", "b")
tmp.Store("b", "c")
tmp.Store("c", "c")
fmt.Printf("%+v
", tmp)
msg, _ := json.Marshal(tmp)
fmt.Println(len(msg))
fmt.Println(msg)
tmp.Range(func(k, v interface{}) bool {
ss, ok1 := k.(string)
vv, ok2 := v.(string)
if ok1 && ok2 {
pp[ss] = vv
}
return true
})
fmt.Println("----0", pp)
bb, err := json.Marshal(pp)
if err==nil {
fmt.Println(string(bb))
}
}
{mu:{state:0 sema:0} read:{v:{m:map[] amended:true}} dirty:map[c:0xc00007a028 a:0xc00007a018 b:0xc00007a020] misses:0} 2 [123 125] ----0 map[a:b b:c c:c] {"a":"b","b":"c","c":"c"}
これはjsonに相当する.Marshal()はsyncに対して標準mapのみをサポートする.Mapは再処理しなければなりません