当前位置:网站首页>Yu Mr Series 】 【 2022 July 022 - Go Go teaching course of container in the dictionary
Yu Mr Series 】 【 2022 July 022 - Go Go teaching course of container in the dictionary
2022-07-31 07:39:00 【Yugong move code】
文章目录
一、GoA dictionary of containers
1.什么是字典
Go Chinese dictionary is also called map , map 是一种无序的键值对的集合,使用散列表(hash)实现.
2.字典的定义
var 变量名 [keyType]valueType
- keyType 表示键类型.
- valueType Indicates the value type corresponding to the key.
2.1 第一种使用方式make
package main
import "fmt"
func main() {
// Define a key type as string,The value type is integer map
m := make(map[int]string)
// 向 map 中添加一个键为 “1”,值为 愚公1号 的映射关系
key := 1
m[key] = "愚公1号"
// 输出 map 中键为 “1” 对应的值
fmt.Println(m[key])
// 声明一个 ok 变量,Used to receive whether the corresponding key exists map 中
value, ok := m[key]
// 如果值不存在,则输出值
if ok {
fmt.Println(value)
}
}
2.2 第二种使用方式{}
package main
import "fmt"
func main() {
// Define a key type as string,The value type is integer map
m := map[int](string){
1: "愚公1号",
2: "愚公2号",
3: "愚公3号",
}
// 输出 map 中键为 “1” 对应的值
fmt.Println(m[1])
// 声明一个 ok 变量,Used to receive whether the corresponding key exists map 中
value, ok := m[2]
// 如果值不存在,则输出值
if ok {
fmt.Println(value)
}
}
The above code is not used make(), Instead, the dictionary is initialized by means of curly braces map, 有点像 JSON 格式一样,To the left of the colon is the key(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 是一种无序的数据结构,The output is out of order and is random.
三、The key-value pairs of the dictionary are deleted
delete(map, 键)
- map Indicates the target to delete map 对象.
- The key indicates what to delete 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的并发问题
GoThe dictionary readonly is thread-safe,同时读写是线程不安全的.
package main
func main() {
// Initialize a key as an integer,The value is also an integer map
m := make(map[int]int)
// 开启一段并发代码
go func() {
// infinite loop to map 里写值
for {
m[1] = 1
}
}()
// 开启一段并发代码
go func() {
// 无限循环读取 map 数据
for {
_ = m[1]
}
}()
// 死循环,Let the above concurrent code execute in the background
for {
}
}
Because of the concurrent pair map 进行读写.Two concurrent functions are constantly paired map There is a race condition for reading and writing.map 内部会对这种并发操作进行检查并提前发现.
2.sync.Map的使用
package main
import (
"fmt"
"sync"
)
func main() {
var m sync.Map
// Add some key-value pairs to 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
})
}
边栏推荐
- codec2 BlockPool:unreadable libraries
- Jobject 使用
- SQLite数据库连接字符串
- MySql的安装配置超详细教程与简单的建库建表方法
- 2022.07.18_每日一题
- 2022.07.14_每日一题
- Automatic translation software - batch batch automatic translation software recommendation
- Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
- MySQL笔记下
- nohup principle
猜你喜欢
Postgresql source code learning (33) - transaction log ⑨ - see the overall process of log writing from the insert record
基于LSTM的诗词生成
2022.07.20_每日一题
电脑开机密码怎么设置?如何给你的电脑加上“安全锁”
Obtaining server and client information
【编程题】【Scratch三级】2022.03 冬天下雪了
Foreign trade website optimization - foreign trade website optimization tutorial - foreign trade website optimization software
双倍数据速率同步动态随机存储器(Double Data Rate Synchronous Dynamic Random Access Memory, DDR SDRAM)- 逻辑描述部分
2022.07.29_每日一题
小实战项目之——吃货联盟订餐系统
随机推荐
Kubernetes scheduling
Gradle剔除依赖演示
深度学习通信领域相关经典论文、数据集整理分享
2022.07.12_每日一题
Kubernetes调度
【编程题】【Scratch三级】2022.03 冬天下雪了
把 VS Code 当游戏机
文件 - 07 删除文件: 根据fileIds批量删除文件及文件信息
服务器和客户端信息的获取
芯塔电子斩获第十一届中国双创大赛芜湖赛区桂冠
【解决】mysql本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
Core Tower Electronics won the championship in the Wuhu Division of the 11th China Innovation and Entrepreneurship Competition
tidyverse笔记——管道函数
tidyverse笔记——dplyr包
事务的四大特性
【并发编程】ReentrantLock的lock()方法源码分析
解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie
04-SDRAM:读操作(突发)
360 push-360 push tool-360 batch push tool
那些破釜沉舟入局Web3.0的互联网精英都怎么样了?