Go Language Map(Collection)
A Map is an unordered collection of key-value pairs.
The most important thing about a Map is that it allows fast data retrieval by key; a key is like an index that points to the value of the data.
A Map is a collection, so we can iterate over it just as we iterate over arrays and slices. However, a Map is unordered, so the order of key-value pairs returned when traversing a Map is indeterminate.
When getting a value from a Map, if the key does not exist, the zero value of that type is returned; for example, the zero value of int is 0, and the zero value of string is "".
A Map is a reference type; if a Map is passed to a function or assigned to another variable, they all point to the same underlying data structure, so modifications to the Map affect all variables that reference it.
Defining a Map
You can use the built-in function make or the map keyword to define a Map:
/* 使用 make 函数 */
map_variable := make(map[KeyType]ValueType, initialCapacity)
Here KeyType is the type of the key, ValueType is the type of the value, and initialCapacity is an optional parameter that specifies the initial capacity of the Map. The capacity of a Map refers to the number of key-value pairs it can hold; when the number of pairs reaches the capacity, the Map automatically expands. If initialCapacity is not specified, Go chooses an appropriate value based on the actual situation.
Example
// Create an empty Map
m
:=
make
(
map
[
string
]
int
)
// Create a Map with an initial capacity of 10
m
:=
make
(
map
[
string
]
int
,
10
)
You can also create a Map using a literal:
// Create Map using literal
m := map[string]int{
"apple": 1,
"banana": 2,
"orange": 3,
}
Get an element:
// 获取键值对
v1 := m["apple"]
v2, ok := m["pear"] // 如果键不存在,ok 的值为 false,v2 的值为该类型的零值
Modify an element:
// 修改键值对
m["apple"] = 5
Get the length of a Map:
// 获取 Map 的长度
len := len(m)
Traverse a Map:
// 遍历 Map
for k, v := range m {
fmt.Printf("key=%s, value=%d\n", k, v)
}
Delete elements:
// 删除键值对
delete(m, "banana")
Example
The following example demonstrates creating and using a map:
Example
package
main
import
"fmt"
func
main
()
{
var
siteMap
map
[
string
]
string
/* Create a collection */
siteMap
=
make
(
map
[
string
]
string
)
/* map inserts key-value pairs, with the capital corresponding to each country */
siteMap
[
"Google"
]
=
"Google"
siteMap
[
"Runoob"
]
=
"Runoob Tutorial"
siteMap
[
"Baidu"
]
=
"Baidu"
siteMap
[
"Wiki"
]
=
"Wikipedia"
/* Use keys to output map values */
for
site
:=
range
siteMap
{
fmt
.
Println
(
site
,
"The capital is"
,
siteMap
[
site
])
}
/* Check whether an element exists in the collection */
name
,
ok
:=
siteMap
[
"Facebook"
]
/* If it is determined to be true, it exists; otherwise it does not */
/*fmt.Println(capital) */
/*fmt.Println(ok) */
if
(
ok
)
{
fmt
.
Println
(
"Facebook's site is"
,
name
)
}
else
{
fmt
.
Println
(
"Facebook site does not exist"
)
}
}
The above example produces the following result:
Wiki 首都是 维基百科
Google 首都是 谷歌
Runoob 首都是 Runoob Tutorial
Baidu 首都是 百度
Facebook 站点不存在
The delete() Function
The delete() function is used to delete elements from a collection; its parameters are the map and the corresponding key. The example is as follows:
Example
package
main
import
"fmt"
func
main
()
{
/* Create a map */
countryCapitalMap
:=
map
[
string
]
string
{
"France"
:
"Paris"
,
"Italy"
:
"Rome"
,
"Japan"
:
"Tokyo"
,
"India"
:
"New delhi"
}
fmt
.
Println
(
"Original map"
)
/* Print the map */
for
country
:=
range
countryCapitalMap
{
fmt
.
Println
(
country
,
"The capital is"
,
countryCapitalMap
[
country
])
}
/Delete elements/
delete
(
countryCapitalMap
,
"France"
)
fmt
.
Println
(
"France entry deleted"
)
fmt
.
Println
(
"Map after deleting the element"
)
/Print the Map/
for
country
:=
range
countryCapitalMap
{
fmt
.
Println
(
country
,
"The capital is"
,
countryCapitalMap
[
country
])
}
}
The above example produces the following result:
原始地图
India 首都是 New delhi
France 首都是 Paris
Italy 首都是 Rome
Japan 首都是 Tokyo
法国条目被删除
删除元素后地图
Italy 首都是 Rome
Japan 首都是 Tokyo
India 首都是 New delhi