当前位置:网站首页>【Golang】快速复习指南QuickReview(三)——map
【Golang】快速复习指南QuickReview(三)——map
2022-06-23 18:52:00 【DDGarfield】
学名:映射关系容器。
俗名:键值对key-value
map跟slice一样,依然是一个引用类型。
map
1.C#中的字典
根据map的特点,博主类比的是C#中字典Dictionary,同样都是键值对。
//定义 初始化
Dictionary<int, string> dic = new Dictionary<int, string>
{
{1,"学生"},
{2,"老师"},
{3,"家长"}
};
//新增
dic.Add(4, "校长");
//判断是否含key
if (dic.ContainsKey(1))
{
}
//判断是否含value
if (dic.ContainsValue("学生")) { }
//遍历字典
foreach (KeyValuePair<int, string> kv in dic)
{
}
//删除指定key的元素
dic.Remove(1);
//清空字典
dic.Clear();
2.Golang中的map
2.1 定义 初始化
make(map[KeyType]ValueType, [cap]),cap可选
testMap := make(map[int]string, 3)
testMap[1] = "学生"
testMap[2] = "老师"
testMap[3] = "家长"
fmt.Println(testMap)
map[1:学生 2:老师 3:家长]
2.2 遍历
使用for k,v:=range map{}遍历:
//遍历
for k, v := range testMap {
fmt.Println(k, v)
}
1 学生
2 老师
3 家长
2.3 删除
删除,使用内置函数delete()
func delete(m map[Key]Type, key Key)
//删除
delete(testMap, 1)
for k, v := range testMap {
fmt.Println(k, v)
}
3 家长
2 老师
2.4 重点来了
切片是一种类型,且使用频繁,切片就也能作为map的值value
var mapValueSlice = make(map[string][]string, 3)
var sc []string
sc = append(sc, "成都", "绵阳", "宜宾")
var sx = []string{"西安", "汉中", "榆林"}
mapValueSlice["四川"] = sc
mapValueSlice["陕西"] = sx
fmt.Println(mapValueSlice)
map[四川:[成都 绵阳 宜宾] 陕西:[西安 汉中 榆林]]
最后说一下,golang的源码读懂需要基础语法很牢固,后面的函数,方法接收者,指针,参数,返回值,通道等等,远远一看,中括号[]横飞,*,其实万变不离其宗,都是这些基础类型按照语义组合而成。
再次强调:这个系列并不是教程,如果想系统的学习,博主可推荐学习资源。
边栏推荐
- Deeply understand and grasp the basic characteristics of digital economy
- Can the biggest gamefi crash victim survive the bear market in May| May Monthly Report
- GaussDB(DWS) 数据库智能监控运维服务-节点监控指标
- 盘点四种WiFi加密标准:WEP、WPA、WPA2、WPA3
- [comparative learning] koa JS, gin and asp Net core - Middleware
- Real topic of the 13th National Competition of single chip microcomputer in the Blue Bridge Cup
- Uniswap founder: no independent token will be issued for genie, and Genie products will be integrated into the uniswap interface
- 八大误区,逐个击破(终篇):云难以扩展、定制性差,还会让管理员失去控制权?
- Leaders of Hangcheng street, Bao'an District and their delegation visited lianchengfa for investigation
- LeetCode 473. 火柴拼正方形
猜你喜欢

Application of JDBC in performance test

Summary of accelerating mobile applications at network edge with software programmable FPGA

Rendering of kotlin jetpack compose tab using animatedvisibility

混沌工程,了解一下

UST 崩盘后,稳定币市场格局将迎来新机遇?

Chaos engineering, learn about it

Naacl 2022 finds | byte proposes MTG: multilingual text generation data set

Crise de 35 ans? Le volume intérieur est devenu synonyme de programmeur...

Online text entity extraction capability helps applications analyze massive text data

ElastricSearch第二弹之分片原理
随机推荐
JS高级程序设计第 4 版:生成器的学习
金鱼哥RHCA回忆录:DO447管理用户和团队的访问--用团队有效地管理用户
Open source SPL redefines OLAP server
20省市公布元宇宙路线图
Advanced network accounting notes (6)
[cloud trends] the four highlights of Huawei cloud store brand new release are here
LeetCode 1079. movable-type printing
Tupu software digital twin intelligent water service, breaking through the development dilemma of sponge City
官宣.NET 7 预览版5
How to avoid the "black swan" incident in the gene field: a security war behind a preventive "recall"
八大误区,逐个击破(终篇):云难以扩展、定制性差,还会让管理员失去控制权?
Are there conditions for making new bonds? Is it safe to make new bonds
Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
Application of JDBC in performance test
LeetCode 1079. 活字印刷
基于SSM实现微博系统
图扑软件数字孪生智慧水务,突破海绵城市发展困境
JDBC 在性能测试中的应用
打新债 要求 打新债安全吗
5 月最大的 GameFi 崩溃受害者能否在熊市中生存?| May Monthly Report