当前位置:网站首页>Sync.map of go language
Sync.map of go language
2022-07-27 11:57:00 【Big leaves are not small】
brief introduction
go The language comes with map, Not thread safe ; So in order to achieve thread safety , have access to sync.Map
See the following code for specific examples
package main
import (
"fmt"
"sync"
)
func main() {
var testMap sync.Map
// obtain a The corresponding key value should fail
valueA,ok := testMap.Load("a")
if ok {
fmt.Println("a", valueA)
} else {
fmt.Println("a no value" ) // true
}
// Set up a Corresponding key value , And then get
testMap.Store("a", "ok1")
valueA,ok = testMap.Load("a")
if ok {
fmt.Println("a", valueA) // true
} else {
fmt.Println("a no value")
}
// Set up a Corresponding key value , And then get
testMap.Store("a", "ok2")
valueA,ok = testMap.Load("a")
if ok {
fmt.Println("a", valueA) // true
} else {
fmt.Println("a no value")
}
// Set up a Corresponding key value , And then get
// if a Corresponding key value exists , Just get , Do not set new values
valueA,ok = testMap.LoadOrStore("a", "ok3")
if ok {
fmt.Println("a", valueA) // true
} else {
fmt.Println("a no value")
}
valueA,ok = testMap.Load("a")
if ok {
fmt.Println("a", valueA) // true
} else {
fmt.Println("a no value")
}
// if a There is no corresponding key value , Then set
valueB,ok := testMap.LoadOrStore("b", "ok3")
if ok {
fmt.Println("b", valueB)
} else {
fmt.Println("b no value") // true
}
valueB,ok = testMap.Load("b")
if ok {
fmt.Println("b", valueB) // true
} else {
fmt.Println("b no value")
}
// Traverse
testMap.Range(func(key, value interface{}) bool {
keyInfo := key.(string)
valueInfo := value.(string)
fmt.Println("keyInfo", keyInfo, "valueInfo", valueInfo)
return true
})
// Delete
testMap.Delete("a")
}
Running results
a no value
a ok1
a ok2
a ok2
a ok2
b no value
b ok3
keyInfo a valueInfo ok2
keyInfo b valueInfo ok3
————————————————
Copyright notice : This paper is about CSDN Blogger 「zerone To change the world 」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/javaxflinux/article/details/89712699
边栏推荐
猜你喜欢

阿里云云数据库RDS版Exception during pool initialization
Unexpected harvest of epic distributed resources, from basic to advanced are full of dry goods, big guys are strong!

The first case of monkeypox in pregnant women in the United States: the newborn was injected with immunoglobulin and was safely born

IDEA: Can‘t use Subversion command line client:svn 解决方案

N ¨UWA: Visual Synthesis Pre-training for Neural visUal World creAtionChenfei

Newton Raphson iterative method

【产品】关于微信产品分析

go入门篇 (4)
![[machine learning whiteboard derivation series] learning notes --- conditional random fields](/img/cc/87d8822b659b31360546adf057ef00.png)
[machine learning whiteboard derivation series] learning notes --- conditional random fields
意外收获史诗级分布式资源,从基础到进阶都干货满满,大佬就是强!
随机推荐
解决方案:Can not issue executeUpdate() or executeLargeUpdate() for SELECTs
Shell脚本文本三剑客之awk
STM32编译出现error: L6235E: More than one section matches selector - cannot all be FIRST/L
w.r.t. ; i.e.;etc.;e.g.是什么意思
Principle, concept and construction process of MySQL database master-slave replication cluster
Greek alphabet reading
Proteus8专业版破解后用数码管闪退的解决
Solution of digital tube flash back after proteus8 professional version cracking
剑指 Offer 笔记: T57 - II. 和为 s 的连续正数序列
解决方案:idea project没有显示树状图
快抖抢救“失意人”
Shell script text three swordsmen sed
Sword finger offer notes: t57 - I. and two numbers of S
Sword finger offer note: t45. arrange the array into the smallest number
Leetcode 01: t1. sum of two numbers; T1108. IP address invalidation; T344. Reverse string
求不同采样周期下的传递函数有限零点
Wilcoxon rank sum and signed rank
配置更改删除了路由过滤器,分布路由器不堪重负:加拿大网络大瘫痪
iptables防火墙
origin如何作一张图中多张子图是柱状图(或其他图)