当前位置:网站首页>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
})
}
边栏推荐
- MySQL系列一:账号管理与引擎
- MySql的安装配置超详细教程与简单的建库建表方法
- 2022.7.29 数组
- 【微服务】Nacos集群搭建以及加载文件配置
- 电压源的电路分析知识分享
- 解决安装 Bun 之后出现 zsh compinit: insecure directories, run compaudit for list. Ignore insecure directorie
- MySQL的触发器
- 多进程全局变量失效、变量共享问题
- One of the small practical projects - food alliance ordering system
- 【 TA - frost Wolf _may - "one hundred plan" 】 art 2.3 hard surface
猜你喜欢
随机推荐
【C语言项目合集】这十个入门必备练手项目,让C语言对你来说不再难学!
Database Principles Homework 2 — JMU
自动翻译软件-批量批量自动翻译软件推荐
2022.07.15_每日一题
2022.07.14_每日一题
测试 思维导图
【面试:并发篇38:多线程:线程池】ThreadPoolExecutor类的基本概念
批量免费文字翻译
【Go】Go 语言切片(Slice)
2022.07.14_每日一题
事务的传播机制
Install the gstreamer development dependency library to the project sysroot directory
PCB抄板
【解决】mysql本地计算机上的MySQL服务启动后停止。某些服务在未由其他服务或程序使用时将自动停止
文件 - 05 下载文件:根据文件Id下载文件
【愚公系列】2022年07月 Go教学课程 022-Go容器之字典
gstreamer's caps event and new_segment event
电压源的电路分析知识分享
[PSQL] 复杂查询
03-SDRAM:写操作(突发)