Go Language Map (Collection)
A Map is an unordered collection of key-value pairs.
The most important aspect of a Map is that it can quickly retrieve data by key. The key acts like an index, pointing to the value of the data.
A Map is a collection, so we can iterate over it just like we iterate over arrays and slices. However, a Map is unordered, and the order of the key-value pairs returned when traversing the Map is not deterministic.
When retrieving a value from a Map, if the key does not exist, the zero value of that type is returned. For example, the zero value for the int type is 0, and the zero value for the string type is "".
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 will affect all variables that reference it.
Define 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 used to specify the initial capacity of the Map. The capacity of a Map refers to the number of key-value pairs it can store. When the number of key-value pairs in the Map reaches the capacity, the Map automatically expands. If initialCapacity is not specified, the Go language will choose an appropriate value based on the actual situation.
Example
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:
// 使用字面量创建 Map
m := map[string]int{
"apple": 1,
"banana": 2,
"orange": 3,
}
Get element:
// 获取键值对 v1 := m["apple"] v2, ok := m["pear"] // 如果键不存在,ok 的值为 false,v2 的值为该类型的零值
Modify element:
// 修改键值对 m["apple"] = 5
Get the length of a Map:
// 获取 Map 的长度 len := len(m)
Iterate over Map:
// 遍历 Map
for k, v := range m {
fmt.Printf("key=%s, value=%d\n", k, v)
}Delete element:
// 删除键值对 delete(m, "banana")
Example
The following example demonstrates creating and using a map:
Example
import "fmt"
func main() {
var siteMap map[string]string /* Create a collection */
siteMap = make(map[string]string)
/* map inserts key-value pairs, the capital corresponding to each country */
siteMap [ "Google" ] = "Google"
siteMap [ "Example" ] = "Example"
siteMap [ "Baidu" ] = "Baidu"
siteMap [ "Wiki" ] = "Wikipedia"
/* Output the map value using the key */
for site := range siteMap {
fmt.Println(site, "The capital is", siteMap [site])
}
/* Check whether the element exists in the collection */
name, ok := siteMap [ "Facebook" ] /* If it is true, it exists; otherwise it does not exist */
/*fmt.Println(capital) */
/*fmt.Println(ok) */
if (ok) {
fmt.Println("Facebook's site is", name)
} else {
fmt.Println("Facebook site does not exist")
}
}
The output of the above example is:
Wiki 首都是 维基百科 Google 首都是 谷歌 Example 首都是 Example Baidu 首都是 百度 Facebook 站点不存在
delete() function
The delete() function is used to delete elements from a collection. Its parameters are the map and its corresponding key. Example:
Example
import "fmt"
func main() {
/* Create map */
countryCapitalMap := map[string]string{"France": "Paris", "Italy": "Rome", "Japan": "Tokyo", "India": "New delhi"}
fmt.Println("Original map")
/* Print map */
for country := range countryCapitalMap {
fmt.Println(country, "The capital is", countryCapitalMap [ country ])
}
/* Delete element */ delete(countryCapitalMap, "France")
fmt.Println("The France entry is deleted")
fmt.Println("Map after deleting element")
/* Print map */
for country := range countryCapitalMap {
fmt.Println(country, "The capital is", countryCapitalMap [ country ])
}
}
The output of the above example is:
原始地图 India 首都是 New delhi France 首都是 Paris Italy 首都是 Rome Japan 首都是 Tokyo 法国条目被删除 删除元素后地图 Italy 首都是 Rome Japan 首都是 Tokyo India 首都是 New delhiother extensions