当前位置:网站首页>【愚公系列】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
})
}

边栏推荐
- 【并发编程】ReentrantLock的lock()方法源码分析
- js原型详解
- What is float?What is document flow?Several ways and principles of clearing floats?What is BFC, how to trigger BFC, the role of BFC
- Redux状态管理
- 自动翻译软件-批量批量自动翻译软件推荐
- 文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
- 【Go语言刷题篇】Go完结篇函数、结构体、接口、错误入门学习
- Third-party library-store
- (border-box)盒子模型w3c、IE的区别
- Zotero | Zotero translator插件更新 | 解决百度学术文献无法获取问题
猜你喜欢

mysql索引失效的常见9种原因详解

DirectExchange交换机简单入门demo

拓扑排序的两种方法 --- dfs+Kahn

In-depth analysis of z-index

Core Tower Electronics won the championship in the Wuhu Division of the 11th China Innovation and Entrepreneurship Competition

MySql的安装配置超详细教程与简单的建库建表方法

Postgresql source code learning (34) - transaction log ⑩ - full page write mechanism

Install and use uView

Analysis of the principle and implementation of waterfall flow layout

Bulk free text translation
随机推荐
Analysis of the implementation principle and detailed knowledge of v-model syntactic sugar and how to make the components you develop support v-model
Moment.js common methods
iOS大厂面试查漏补缺
Core Tower Electronics won the championship in the Wuhu Division of the 11th China Innovation and Entrepreneurship Competition
讲解实例+详细介绍@Resource与@Autowired注解的区别(全网最全)
In-depth analysis of z-index
【云原生】3.3 Kubernetes 中间件部署实战
【微服务】Nacos集群搭建以及加载文件配置
CHI论文阅读(1)EmoGlass: an End-to-End AI-Enabled Wearable Platform for Enhancing Self-Awareness of Emoti
Jobject 使用
在 ASP.NET Core 应用程序启动时运行代码的 3 种方法
Bulk free text translation
深度解析 z-index
Kubernetes调度
数据库原理作业3 — JMU
Install the gstreamer development dependency library to the project sysroot directory
一文读懂 MongoDB 和 MySQL 的差异
从入门到一位合格的爬虫师,这几点很重要
浅析伪类和伪元素
知识、创新、回报。