【Golang语言社区】GO1.9 map并发安全测试

var m sync.Map //全局 func maintest() { // 第一个 YongHuomap := make(map[string]StPaiHangBangPC) var test1 StPaiHangBangPC test1.OpenID = "openid_1" test1.PaiHang = "1" test1.YaoCiShu = "10" YongHuomap["openid_1"] = test1 m.Store("ShangJiaOenID_1", YongHuomap) // 第二个 YongHuomap2 := make(map[string]StPaiHangBangPC) var test2 StPaiHangBangPC test2.OpenID = "openid_2" test2.PaiHang = "2" test2.YaoCiShu = "9" YongHuomap2["openid_2"] = test2 var test3 StPaiHangBangPC test3.OpenID = "openid_3" test3.PaiHang = "1" test3.YaoCiShu = "10" YongHuomap2["openid_3"] = test3 var test5 StPaiHangBangPC test5.OpenID = "openid_4" test5.PaiHang = "1" test5.YaoCiShu = "1000099999" var test4 StPaiHangBangPC test4.OpenID = "openid_4" test4.PaiHang = "1" test4.YaoCiShu = "10" YongHuomap2["openid_4"] = test4 // 获取存的数据 rdata3, sss := m.Load("ShangJiaOenID_2") if sss { hdata := make(map[string]StPaiHangBangPC) for _, vvv11 := range rdata3.(interface{}).(map[string]StPaiHangBangPC) { var YongHuomapLinShi StPaiHangBangPC YongHuomapLinShi.OpenID = vvv11.OpenID YongHuomapLinShi.YaoCiShu = vvv11.YaoCiShu hdata[YongHuomapLinShi.OpenID] = YongHuomapLinShi } fmt.Println(hdata) hdata[test5.OpenID] = test5 m.Store("ShangJiaOenID_2", hdata) fmt.Println(hdata) vv, _ := m.LoadOrStore("ShangJiaOenID_2", YongHuomap2) fmt.Println(vv) } else { m.Store("ShangJiaOenID_2", YongHuomap2) } rdata4, sss := m.Load("ShangJiaOenID_2") if sss { hdata := make(map[string]StPaiHangBangPC) for _, vvv11 := range rdata4.(interface{}).(map[string]StPaiHangBangPC) { var YongHuomapLinShi StPaiHangBangPC YongHuomapLinShi.OpenID = vvv11.OpenID YongHuomapLinShi.YaoCiShu = vvv11.YaoCiShu hdata[YongHuomapLinShi.OpenID] = YongHuomapLinShi } fmt.Println(hdata) hdata[test5.OpenID] = test5 m.Store("ShangJiaOenID_2", hdata) fmt.Println(hdata) vv, _ := m.LoadOrStore("ShangJiaOenID_2", YongHuomap2) fmt.Println(vv) } else { m.Store("ShangJiaOenID_2", YongHuomap2) } } 运行结果:

原文发布于微信公众号 - Golang语言社区(Golangweb)

原文发表时间:2017-07-07

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

扫码关注云+社区