当前位置:网站首页>【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的源码读懂需要基础语法很牢固,后面的函数,方法接收者,指针,参数,返回值,通道等等,远远一看,中括号[]横飞,*,其实万变不离其宗,都是这些基础类型按照语义组合而成。
再次强调:这个系列并不是教程,如果想系统的学习,博主可推荐学习资源。
边栏推荐
- 混沌工程,了解一下
- Stochastic process -- Markov chain
- Add two factor authentication, not afraid of password disclosure, let alone 123456
- Save: software analysis, verification and test platform
- Shell Scripting
- What are the requirements for new bonds? Is it safe to play new bonds
- The golden nine silver ten, depends on this detail, the offer obtains the soft hand!
- How to install SSL certificates in Microsoft Exchange 2010
- Can the biggest gamefi crash victim survive the bear market in May| May Monthly Report
- Naacl 2022 finds | byte proposes MTG: multilingual text generation data set
猜你喜欢

Open source SPL redefines OLAP server

Helix QAC更新至2022.1版本,将持续提供高标准合规覆盖率

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

Live broadcast review | detailed explanation of koordinator architecture of cloud native hybrid system (complete ppt attached)

开源 SPL 重新定义 OLAP Server

Why is only one value displayed on your data graph?

The golden nine silver ten, depends on this detail, the offer obtains the soft hand!

vs2022scanf函数的使用,使用scanf的报错-返回值被忽略:解决·方法

JS高级程序设计第 4 版:生成器的学习

科班出身,结果外包都不要
随机推荐
Hardware development notes (6): basic process of hardware development, making a USB to RS232 module (5): creating USB package library and associating principle graphic devices
如何在Microsoft Exchange 2010中安装SSL证书
深入理解和把握数字经济的基本特征
80% of people will be wrong about the three counter intuitive questions?
ElastricSearch第二弹之分片原理
1、 Summary and introduction
Helix QAC更新至2022.1版本,将持续提供高标准合规覆盖率
20省市公布元宇宙路线图
LeetCode 260. Number III that appears only once
Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL
[comparative learning] koa JS, gin and asp Net core - Middleware
Is PMP useful?
20 provinces and cities announce the road map of the meta universe
LeetCode 1079. movable-type printing
Daily question brushing record (II)
How to write a great online user manual in 7 steps
GaussDB(DWS) 数据库智能监控运维服务-节点监控指标
金九银十,靠这个细节,offer拿到手软!
打新债需要具备什么条件 打新债安全吗
Online text entity extraction capability helps applications analyze massive text data