当前位置:网站首页>【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的源码读懂需要基础语法很牢固,后面的函数,方法接收者,指针,参数,返回值,通道等等,远远一看,中括号[]横飞,*,其实万变不离其宗,都是这些基础类型按照语义组合而成。
再次强调:这个系列并不是教程,如果想系统的学习,博主可推荐学习资源。
边栏推荐
- 如何通过7个步骤编写出色的在线用户手册
- What are the requirements for new bonds? Is it safe to play new bonds
- [cloud trends] the four highlights of Huawei cloud store brand new release are here
- Rstudio 1.4 software installation package and installation tutorial
- Technology sharing | wvp+zlmediakit realizes streaming playback of camera gb28181
- 火线沙龙第26期-多云安全专场
- Deeply understand and grasp the basic characteristics of digital economy
- JDBC 在性能測試中的應用
- 硬件开发笔记(六): 硬件开发基本流程,制作一个USB转RS232的模块(五):创建USB封装库并关联原理图元器件
- ElastricSearch第二弹之分片原理
猜你喜欢

Robust extraction of specific signals with time structure (Part 2)

增加双因素认证,不惧密码泄露,更不惧123456

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

35 year old crisis? It has become a synonym for programmers

Save: software analysis, verification and test platform

Interpreting the 2022 agile coaching industry status report

Ready to migrate to the cloud? Please accept this list of migration steps

Convex optimization notes

Rstudio 1.4 software installation package and installation tutorial

解读2022年度敏捷教练行业现状报告
随机推荐
Programmable, protocol independent software switch (read the paper)
ZABBIX monitoring - Aruba AP operation data
ElastricSearch第二弹之分片原理
为什么你的数据图谱分析图上只显示一个值?
基于微信小程序的婚纱影楼小程序开发笔记
TCP/UDP基本原理
【Golang】快速复习指南QuickReview(四)——函数
如何在Microsoft Exchange 2010中安装SSL证书
Can the biggest gamefi crash victim survive the bear market in May| May Monthly Report
盘点四种WiFi加密标准:WEP、WPA、WPA2、WPA3
宝安区航城街道领导一行莅临联诚发参观调研
Kinsoku jikou desu新浪股票接口变动
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
logstash启动 -r 参数
Gaussdb (DWS) database intelligent monitoring operation and maintenance service - node monitoring indicators
Zabbix监控- Aruba AP运行数据
硬件开发笔记(六): 硬件开发基本流程,制作一个USB转RS232的模块(五):创建USB封装库并关联原理图元器件
1、 Summary and introduction
What conditions do you need to meet to fight new debts? Is it safe to fight new debts
Interview with Mo Tianlun | ivorysql wangzhibin - ivorysql, an Oracle compatible open source database based on PostgreSQL