# map

map是一堆未排序键值对的集合，map的key必须为可比较类型,比如 ==!=，map查找比线性查找快,但慢于索引查找(数组，切片)

### 定义一个map

``````var a map[int]int
var b = map[string]string{}
c := map[string]bool{}
d := make(map[string]int)
``````

### map的初始化

map类似于数组和切片,可以在定义时直接指定初始值 如:

``````var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
``````

### map元素访问

``````var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
fmt.Println(a["name"],a["age"])
``````

``````var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
fmt.Println(a["name"], a["age"], a["parent"])
``````

map中并没有parent这个键,在诸如C++/Java或其他语言中,如果访问一个并不存在的键,将直接导致程序异常,所以判断一个map中是否存在某个键,就成了必需的步骤，但在golang中，这些都不是问题

``````package main

import (
"fmt"
)

func main() {
var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
fmt.Println(a["parent"])
if a["parent"] == "" {
fmt.Println("yes")
}

var b = map[int]int{1: 1, 2: 2}
fmt.Println(b[3])
if b[3] == 0 {
fmt.Println("yes")
}
}
``````

//这个是空字符串
yes
0
yes

``````func main() {
var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
if value, ok := a["parent"]; ok == true {
fmt.Println(value)
} else {
fmt.Println("key not in map")
}
}
``````

### 修改map中的元素

``````var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
fmt.Println(a)
a["name"] = "李四"
fmt.Println(a)
``````

map[name:zhangsan age:16 sex:男]
map[name:李四 age:16 sex:男]

### 新增元素

``````package main

import (
"fmt"
)

func main() {
var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
a["girlfriend"] = "苍老师"
fmt.Println(a)
}
``````

### 删除元素

``````package main

import (
"fmt"
)

func main() {
var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
delete(a, "name")
fmt.Println(a)
}
``````

### map是引用类型

map是引用类型,来看一个简单的例子

``````func main() {
var a = map[int]int{1: 1, 2: 2}
b := a
a[1] = 123456
fmt.Println(a, b)
}
``````

``````func main() {
var a = map[int]int{1: 1, 2: 2}
b := a
test(a)
fmt.Println(a, b)
}

func test(a map[int]int) {
a[1] = 111
}
``````

### map的遍历

``````var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
for key, value := range a {
fmt.Println(key, value)
}
``````

1 回复  |  直到 2019-08-05 15:41:33

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

map是一堆未排序键值对的集合，map的key必须为可比较类型,比如 ==!=，map查找比线性查找快,但慢于索引查找(数组，切片)

### 定义一个map

``````var a map[int]int
var b = map[string]string{}
c := map[string]bool{}
d := make(map[string]int)
``````

### map的初始化

map类似于数组和切片,可以在定义时直接指定初始值 如:

``````var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
``````

### map元素访问

``````var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
fmt.Println(a["name"],a["age"])
``````

``````var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
fmt.Println(a["name"], a["age"], a["parent"])
``````

map中并没有parent这个键,在诸如C++/Java或其他语言中,如果访问一个并不存在的键,将直接导致程序异常,所以判断一个map中是否存在某个键,就成了必需的步骤，但在golang中，这些都不是问题

``````package main

import (
"fmt"
)

func main() {
var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
fmt.Println(a["parent"])
if a["parent"] == "" {
fmt.Println("yes")
}

var b = map[int]int{1: 1, 2: 2}
fmt.Println(b[3])
if b[3] == 0 {
fmt.Println("yes")
}
}
``````

//这个是空字符串
yes
0
yes

``````func main() {
var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
if value, ok := a["parent"]; ok == true {
fmt.Println(value)
} else {
fmt.Println("key not in map")
}
}
``````

### 修改map中的元素

``````var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
fmt.Println(a)
a["name"] = "李四"
fmt.Println(a)
``````

map[name:zhangsan age:16 sex:男]
map[name:李四 age:16 sex:男]

### 新增元素

``````package main

import (
"fmt"
)

func main() {
var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
a["girlfriend"] = "苍老师"
fmt.Println(a)
}
``````

### 删除元素

``````package main

import (
"fmt"
)

func main() {
var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
delete(a, "name")
fmt.Println(a)
}
``````

### map是引用类型

map是引用类型,来看一个简单的例子

``````func main() {
var a = map[int]int{1: 1, 2: 2}
b := a
a[1] = 123456
fmt.Println(a, b)
}
``````

``````func main() {
var a = map[int]int{1: 1, 2: 2}
b := a
test(a)
fmt.Println(a, b)
}

func test(a map[int]int) {
a[1] = 111
}
``````

### map的遍历

``````var a = map[string]string{"name": "zhangsan", "age": "16", "sex": "男"}
for key, value := range a {
fmt.Println(key, value)
}
``````