当前位置:网站首页>【愚公系列】2022年07月 Go教学课程 022-Go容器之字典
【愚公系列】2022年07月 Go教学课程 022-Go容器之字典
2022-07-31 06:06:00 【愚公搬代码】
文章目录
一、Go容器之字典
1.什么是字典
Go 中字典也叫做 map , map 是一种无序的键值对的集合,使用散列表(hash)实现。
2.字典的定义
var 变量名 [keyType]valueType
- keyType 表示键类型。
- valueType 表示键对应的值类型。
2.1 第一种使用方式make
package main
import "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 main
import "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 main
import "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 main
import "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 main
func 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 main
import (
"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
})
}
边栏推荐
猜你喜欢
Bulk free text translation
《白帽子说Web安全》思维导图
Basic usage of Koa framework
Explain the example + detail the difference between @Resource and @Autowired annotations (the most complete in the entire network)
Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
关于求反三角函数的三角函数值
Core Tower Electronics won the championship in the Wuhu Division of the 11th China Innovation and Entrepreneurship Competition
机器学习反向传播的一些推导公式
讲解实例+详细介绍@Resource与@Autowired注解的区别(全网最全)
【科普向】5G核心网架构和关键技术
随机推荐
Jobject 使用
事务的四大特性
Chapter 16: Constructing the Magic Square for Prime Numbers of Order n(5,7)
DDL+DML+DQL
强化学习科研知识必备(数据库、期刊、会议、牛人)
项目 - 如何根据最近30天、最近14天、最近7天、最近24小时、自定义时间范围查询MySQL中的数据?
安装和使用uView
How to choose a suitable UI component library in uni-app
线程中断方法
Redux state management
解决win11/win10在登陆界面(解锁界面)点击获取每日壁纸无效的问题 - get Daily Lockscreen and Wallpaper - Win11/10的登录界面背景图片在哪里?
tidyverse笔记——管道函数
Postgresql source code learning (34) - transaction log ⑩ - full page write mechanism
Detailed explanation of js prototype
Web浏览器工作流程解析
SQL Server Datetime2数据类型
树状数组(单点修改区间查询和区间修改单点查询)
Markdown中的数学符号
试题 历届真题 错误票据【第四届】【省赛】【B组】
Gradle remove dependency demo