当前位置:网站首页>【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的源码读懂需要基础语法很牢固,后面的函数,方法接收者,指针,参数,返回值,通道等等,远远一看,中括号[]横飞,*,其实万变不离其宗,都是这些基础类型按照语义组合而成。
再次强调:这个系列并不是教程,如果想系统的学习,博主可推荐学习资源。
边栏推荐
- String Day6 of Li Kou daily practice
- Application of JDBC in performance test
- 打新债好不好 打新债安全吗
- 如何避免基因领域“黑天鹅”事件:一场预防性“召回”背后的安全保卫战
- UGeek大咖说 | 可观测之超融合存储系统的应用与设计
- [cloud trends] the four highlights of Huawei cloud store brand new release are here
- How to use the low code platform of the Internet of things for process management?
- 如何在Microsoft Exchange 2010中安装SSL证书
- Matrix analysis notes (II)
- Not only Lei Jun, iqoo product manager, praised Qualcomm Xiaolong 8+: a new look
猜你喜欢

基于微信小程序的婚纱影楼小程序开发笔记

游戏资产复用:更快找到所需游戏资产的新方法

LeetCode 260. 只出现一次的数字 III

How to write a great online user manual in 7 steps

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

Not only Lei Jun, iqoo product manager, praised Qualcomm Xiaolong 8+: a new look

Convex optimization notes

Tcp/udp Fundamentals

Eight misunderstandings, broken one by one (final): the cloud is difficult to expand, the customization is poor, and the administrator will lose control?

Programmable, protocol independent software switch (read the paper)
随机推荐
1、 Summary and introduction
Design of hardware switch with programmable full function rate limiter
The evolution of the "Rainbow Bridge" middleware platform for the acquisition database based on shardingsphere
JDBC 在性能測試中的應用
Netseer: stream event telemetry notes for programmable data plane
Logstash start -r parameter
Check four WiFi encryption standards: WEP, WPA, WPA2 and WPA3
Robust extraction of specific signals with time structure (Part 2)
Shell Scripting
金鱼哥RHCA回忆录:DO447管理用户和团队的访问--用团队有效地管理用户
ElastricSearch第二弹之分片原理
打新债好不好 打新债安全吗
[comparative learning] koa JS, gin and asp Net core - Middleware
宝安区航城街道领导一行莅临联诚发参观调研
Programmable data plane (paper reading)
小程序开发框架推荐
QGIS import WMS or WMTs
GaussDB(DWS) 数据库智能监控运维服务-节点监控指标
Kubernetes resource topology aware scheduling optimization
LeetCode 473. 火柴拼正方形