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)
}
}
运行结果: