当前位置:网站首页>Go基础笔记_4_map
Go基础笔记_4_map
2022-07-24 22:53:00 【xcSpark】
map
1. 介绍
map 是一种特殊的数据结构,一种键值对(key-value)的无序集合。和数组切片不同,数组切片是有序的。
//map_name map变量名, key_type键类型,value_type 键对应的值类型
var map_name = make(map[key_type]value_type)
value_type 可以是切片
- key不允许重复,不能是函数、映射或切片
- map可以动态增长,未初始化时值是 nil
map中的键和值均不限制数据类型,键和值可分别使用不同的类型同一个map中所有键类型相同,所有值类型相同
// 如
map[string]int
map[*T]struct{
x, y float64 }
map[string]interface{
}
2. map使用
package main
import "fmt"
func main() {
//创建map
var dept = make(map[string]string)
dept["A"] = "部门1"
dept["B"] = "部门2"
dept["C"] = "部门3"
fmt.Println(dept)
}
// 结果,这里所有的键是string类型,所有的value是string类型
map[A:部门1 B:部门2 C:部门3]

注意:不能使用new创建map
如下编译时报错,得到一个空引用的指针,类似声明了一个未初始化的变量并且取了它的地址
3. 修改map中的值
如下,修改map中的值,重新赋值即可
package main
import "fmt"
func main() {
var dept = make(map[string]string)
// 赋值
dept["A"] = "部门1"
dept["B"] = "部门2"
dept["C"] = "部门3"
fmt.Println(dept)
// 修改C的值
dept["C"] = "部门CCC"
fmt.Println(dept)
}
// 结果
map[A:部门1 B:部门2 C:部门3]
map[A:部门1 B:部门2 C:部门CCC]
4. map扩容
因此它不存在固定长度或者最大限制,但是也可以选择标明 map 的初始容量 capacity
make(map[key_type]value_type, cap)
var a = make(map[int]int, 3)
5. 应用
访问
package main
import "fmt"
func main() {
var a = make(map[int]int, 3)
a[0] = 1
a[1] = 2
a[2] = 3
a[3] = 4
for key, value := range a {
fmt.Println("key:", key, "value:", value)
}
}
// 结果
key: 0 value: 1
key: 1 value: 2
key: 2 value: 3
key: 3 value: 4
边栏推荐
- Flex layout
- 【1184. 公交站间的距离】
- Org.json Jsonexception: what about no value for value
- Update structure of maximum or minimum value in the window - maximum value in the window
- 基于TensorFlow和Keras的卷积神经网络实现猫狗数据集分类实验
- 百度网盘+Chrom插件
- ASP.NET Core 6.0 基于模型验证的数据验证
- 基于FPGA的VGA显示
- 新手哪个证券开户最好 开户最安全
- Kubernetes scheduling concept and workflow
猜你喜欢

Archsummit: evolution of the underlying framework of cherished microservices

基于FPGA的VGA显示

QT6 with vs Code: compiling source code and basic configuration

一文读懂Elephant Swap的LaaS方案的优势之处
![洛谷 P2024 [NOI2001] 食物链](/img/7f/6ccbc19942f0d4a153025346496834.png)
洛谷 P2024 [NOI2001] 食物链

Piziheng embedded: the method of making source code into lib Library under MCU Xpress IDE and its difference with IAR and MDK

Baidu online disk +chrome plug-in

The kettle job implementation runs a kettle conversion task every 6S

物联网平台返回数据解析时遇到org.json.JSONException: No value for Value怎么办

用VS Code搞Qt6:编译源代码与基本配置
随机推荐
百度网盘+Chrom插件
Monotonic stack structure
On the problem that the on-board relay cannot be switched on due to insufficient power supply
【云原生之kubernetes】kubernetes集群高级资源对象statefulesets
Oracle中实现对指定数据分组且获取重复次数
The specified data is grouped and the number of repetitions is obtained in Oracle
CA证书制作实战
P3201 [HNOI2009] 梦幻布丁 启发式合并
DDoS attack classification
Network Security Learning (I) virtual machine
About the word 'don't remember'
Notes of Teacher Li Hongyi's 2020 in-depth learning series lecture 1
【1184. 公交站间的距离】
高阶产品如何提出有效解决方案?(1方法论+2案例+1清单)
Pointrender parsing
Flex layout
用VS Code搞Qt6:编译源代码与基本配置
The rule created by outlook mail is invalid. Possible reasons
"Fundamentals of program design" Chapter 10 function and program structure 7-3 recursive realization of reverse order output integer (15 points)
How static code analysis works