当前位置:网站首页>go语言之sync.Map
go语言之sync.Map
2022-07-27 11:03:00 【大叶子不小】
简介
go语言自带的map,不具备线程安全;所以为了实现线程安全,可以使用sync.Map
具体示例见如下代码
package main
import (
"fmt"
"sync"
)
func main() {
var testMap sync.Map
// 获取a对应的键值应该失败
valueA,ok := testMap.Load("a")
if ok {
fmt.Println("a", valueA)
} else {
fmt.Println("a no value" ) // true
}
// 设置a对应的键值,然后再获取
testMap.Store("a", "ok1")
valueA,ok = testMap.Load("a")
if ok {
fmt.Println("a", valueA) // true
} else {
fmt.Println("a no value")
}
// 设置a对应的键值,然后再获取
testMap.Store("a", "ok2")
valueA,ok = testMap.Load("a")
if ok {
fmt.Println("a", valueA) // true
} else {
fmt.Println("a no value")
}
// 设置a对应的键值,然后再获取
// 若a存在对应键值,则只是获取,不设置新值
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")
}
// 若a不存在对应键值,则进行设置
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")
}
// 遍历
testMap.Range(func(key, value interface{}) bool {
keyInfo := key.(string)
valueInfo := value.(string)
fmt.Println("keyInfo", keyInfo, "valueInfo", valueInfo)
return true
})
// 删除
testMap.Delete("a")
}
运行结果
a no value
a ok1
a ok2
a ok2
a ok2
b no value
b ok3
keyInfo a valueInfo ok2
keyInfo b valueInfo ok3
————————————————
版权声明:本文为CSDN博主「zerone改变世界」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/javaxflinux/article/details/89712699
边栏推荐
- Modelarts voice detection and text classification
- Maker Hongmeng application development training notes 03
- compute_class_weight() takes 1 positional argument but 3 were given
- 剑指 Offer 笔记: T39. 数组中出现次数超过一半的数字
- 【无标题】多模态模型 CLIP
- 局域网SDN硬核技术内幕 24 展望未来——RDMA(中)
- LAN SDN technology hard core insider 11 the key of cloud convergence CP -- hierarchical port binding
- 求不同采样周期下的传递函数有限零点
- LNMP架构搭建(部署Discuz论坛)
- JS-寄生组合式继承
猜你喜欢
Synchronous use reference of the new version of data warehouse (for beginners)

JS-寄生组合式继承

Newton Raphson iterative method

VSCode复制代码时去掉样式/语法高亮/代码高亮/黑色背景

日本福岛废堆安全监视协议会认可排海计划“安全”

Beyond Compare 3 下一个差异段/向下搜索箭头 找不到了

Vscode removes style / syntax highlighting / code highlighting / black background when copying code

微博评论爬虫+可视化

解决方案:idea project没有显示树状图

Introduction to box diagram
随机推荐
Sword finger offer notes: t58 - I. flip word order
Newton-Raphson迭代法
torch‘ has no attribute ‘inference_mode‘
The first case of monkeypox in pregnant women in the United States: the newborn was injected with immunoglobulin and was safely born
Detailed explanation of MATLAB S-function
[machine learning whiteboard derivation series] learning notes - support vector machine and principal component analysis
zabbix自定义监控项
How to make a graph? Multiple subgraphs in a graph are histogram (or other graphs)
JS-寄生组合式继承
剑指 Offer 笔记: T53 - I. 在排序数组中查找数字
Several banks adjusted the redemption rules of cash management financial products: the confirmation time limit of redemption changed from "t+0" to "t+1"
Firewalld防火墙
基于反馈率的控制系统原理
[unity entry program] creator kitfps: first person shooting 3D game
STM32 compilation error: l6235e: more than one section matches selector - cannot all be first/l
The C programming language 2nd -- Notes -- 6.7
希腊字母读法
PWM的原理和PWM波的产生
Arduino常见供电问题与解决
Can you really write binary search - variant binary search