当前位置:网站首页>【Yugong Series】July 2022 Go Teaching Course 022-Dictionary of Go Containers
【Yugong Series】July 2022 Go Teaching Course 022-Dictionary of Go Containers
2022-07-31 16:39:00 【Huawei cloud】
一、Go容器之字典
1.什么是字典
Go 中字典也叫做 map , map 是一种无序的键值对的集合,使用散列表(hash)实现.
2.字典的定义
var 变量名 [keyType]valueType- keyType 表示键类型.
- valueType 表示键对应的值类型.
2.1 第一种使用方式make
package mainimport "fmt"func main() { // 定义一个键类型为字符串,值类型为整型的 map m := make(map[int]string) // 向 map 中添加一个键为 “1”,值为 愚公1号 的映射关系 key := 1 m[key] = "愚公1号" // 输出 map 中键为 “1” 对应的值 fmt.Println(m[key]) // 声明一个 ok 变量,用来接收对应键是否存在于 map 中 value, ok := m[key] // 如果值不存在,则输出值 if ok { fmt.Println(value) }}
2.2 第二种使用方式{}
package mainimport "fmt"func main() { // 定义一个键类型为字符串,值类型为整型的 map m := map[int](string){ 1: "愚公1号", 2: "愚公2号", 3: "愚公3号", } // 输出 map 中键为 “1” 对应的值 fmt.Println(m[1]) // 声明一个 ok 变量,用来接收对应键是否存在于 map 中 value, ok := m[2] // 如果值不存在,则输出值 if ok { fmt.Println(value) }}
上面的这段代码并没有使用 make(), 而是通过大括号的方式来初始化字典 map, 有点像 JSON 格式一样,冒号左边的是键(key) , 右边的是值(value) ,键值对之间使用逗号分隔.
二、字典的遍历
package mainimport "fmt"func main() { m := map[int](string){ 1: "愚公1号", 2: "愚公2号", 3: "愚公3号", } // 通过 for range 遍历, 获取 key, value 值并打印 for key, value := range m { fmt.Printf("key: %d, value: %s\n", key, value) }}
注意: 字典 map 是一种无序的数据结构,输出是不按顺序是随机的.
三、字典的键值对删除
delete(map, 键)- map 表示要删除的目标 map 对象.
- 键表示要删除的 map 中 key 键.
相关案例:
package mainimport "fmt"func main() { m := map[int](string){ 1: "愚公1号", 2: "愚公2号", 3: "愚公3号", } // 删除 map 中键为 1 的键值对 delete(m, 1) // 通过 for range 遍历, 获取 key, value 值并打印 for key, value := range m { fmt.Println(key, value) }}
四、异步sync.Map
1.map的并发问题
Go的字典只读是线程安全的,同时读写是线程不安全的.
package mainfunc main() { // 初始化一个键为整型,值也为整型的 map m := make(map[int]int) // 开启一段并发代码 go func() { // 无限循环往 map 里写值 for { m[1] = 1 } }() // 开启一段并发代码 go func() { // 无限循环读取 map 数据 for { _ = m[1] } }() // 死循环,让上面的并发代码在后台执行 for { }}
因为并发的对 map 进行读写.两个并发函数不断的对 map 进行读写发生了竞态问题.map 内部会对这种并发操作进行检查并提前发现.
2.sync.Map的使用
package mainimport ( "fmt" "sync")func main() { var m sync.Map // 添加一些键值对到 map 中 m.Store(1, "愚公1号") m.Store(2, "愚公2号") m.Store(3, "愚公3号") // 从 sync.Map 中获取键为 2 的值 fmt.Println(m.Load(2)) // 删除键值对 m.Delete(1) // 遍历 sync.Map 中的键值对 m.Range(func(key, value interface{}) bool { fmt.Printf("key: %d, value: %s\n", key, value) return true })}
边栏推荐
- 上传图片-微信小程序(那些年的坑记录2022.4)
- ansible学习笔记02
- 百度网盘网页版加速播放(有可用的网站吗)
- SringMVC中个常见的几个问题
- 牛客网刷题(三)
- After Grafana is installed, the web opens and reports an error
- i.MX6ULL驱动开发 | 33 - NXP原厂网络设备驱动浅读(LAN8720 PHY)
- Handling write conflicts under multi-master replication (4) - multi-master replication topology
- Emmet syntax
- 无主复制系统(1)-节点故障时写DB
猜你喜欢

i.MX6ULL驱动开发 | 33 - NXP原厂网络设备驱动浅读(LAN8720 PHY)

阿里三面:MQ 消息丢失、重复、积压问题,如何解决?
![[TypeScript] In-depth study of TypeScript type operations](/img/d9/ee240ccba72e8d3114ee5c52ed0c8f.png)
[TypeScript] In-depth study of TypeScript type operations

Premiere Pro 2022 for (pr 2022)v22.5.0
![[pytorch] 1.7 pytorch and numpy, tensor and array conversion](/img/ca/b943ff8f59f08e9e23b1ba416c79a0.png)
[pytorch] 1.7 pytorch and numpy, tensor and array conversion

【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】

GP 6 overall architecture study notes

Visualize GraphQL schemas with GraphiQL

"Autumn Recruitment Series" MySQL Interview Core 25 Questions (with answers)

利用PHP开发具有注册、登陆、文件上传、发布动态功能的网站
随机推荐
Flutter gets the height of the status bar statusbar
.NET 20th Anniversary Interview - Zhang Shanyou: How .NET technology empowers and changes the world
联邦学习:联邦场景下的多源知识图谱嵌入
update data table update
C语言-函数
第05章 存储引擎【1.MySQL架构篇】【MySQL高级】
Mariabackup implements incremental data backup for Mariadb 10.3
【愚公系列】2022年07月 Go教学课程 021-Go容器之切片操作
牛客 HJ18 识别有效的IP地址和掩码并进行分类统计
基于C语言的编译器设计与实现
动态规划之线性dp(下)
牛客 HJ20 密码验证合格程序
Applicable scenario of multi-master replication (2) - client and collaborative editing that require offline operation
EF Core 2.2中将ORM框架生成的SQL语句输出到控制台
Oracle dynamically registers non-1521 ports
2.索引及调优篇【mysql高级】
Implementing distributed locks based on Redis (SETNX), case: Solving oversold orders under high concurrency
How does automated testing create business value?
Design and Implementation of Compiler Based on C Language
Summary of the implementation method of string inversion "recommended collection"