[Go] Go Language Tutorial--Go Language Map (Collection) (16)

Past review:

Article Directory

A Map is an unordered collection of key-value pairs.

The most important point of Map is to quickly retrieve data through the key, which is similar to an index and points to the value of the data.

Map is a kind of collection, so we can iterate over it like arrays and slices. However, the Map is unordered, and the order of the key-value pairs returned when traversing the Map is uncertain.

When getting the value of the Map, if the key does not exist, return the zero value of this type, for example, the zero value of the int type is 0, and the zero value of 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 refer to it.

Define Map

Maps can be defined using the built-in function make or using the map keyword:

/* 使用 make 函数 */
map_variable := make(map[KeyType]ValueType, initialCapacity)

Among them, KeyType is the type of the key, ValueType is the type of the value, and initialCapacity is an optional parameter, which is used to specify the initial capacity of the Map. The capacity of the Map refers to the number of key-value pairs that can be stored in the Map. When the number of key-value pairs in the Map reaches the capacity, the Map will automatically expand. If initialCapacity is not specified, Go language will choose an appropriate value according to the actual situation.

example

// 创建一个空的 Map
m := make(map[string]int)

// 创建一个初始容量为 10 的 Map
m := make(map[string]int, 10)
也可以使用字面量创建 Map:

// 使用字面量创建 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
获取 Map 的长度:

// 获取 Map 的长度
len := len(m)

Traverse the Map:

// 遍历 Map
for k, v := range m {
    
    
    fmt.Printf("key=%s, value=%d\n", k, v)
}

Remove element:

// 删除键值对
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 /*创建集合 */
    siteMap = make(map[string]string)

    /* map 插入 key - value 对,各个国家对应的首都 */
    siteMap [ "Google" ] = "谷歌"
    siteMap [ "Baidu" ] = "百度"
    siteMap [ "Wiki" ] = "维基百科"

    /*使用键输出地图值 */
    for site := range siteMap {
    
    
        fmt.Println(site, "首都是", siteMap [site])
    }

    /*查看元素在集合中是否存在 */
    name, ok := siteMap [ "Facebook" ] /*如果确定是真实的,则存在,否则不存在 */
    /*fmt.Println(capital) */
    /*fmt.Println(ok) */
    if (ok) {
    
    
        fmt.Println("Facebook 的 站点是", name)
    } else {
    
    
        fmt.Println("Facebook 站点不存在")
    }
}

The result of running the above example is:

Wiki 首都是 维基百科
Google 首都是 谷歌
Baidu 首都是 百度
Facebook 站点不存在

delete() function

The delete() function is used to delete the elements of the collection, and the parameters are map and its corresponding key. Examples are as follows:

example

package main

import "fmt"

func main() {
    
    
        /* 创建map */
        countryCapitalMap := map[string]string{
    
    "France": "Paris", "Italy": "Rome", "Japan": "Tokyo", "India": "New delhi"}

        fmt.Println("原始地图")

        /* 打印地图 */
        for country := range countryCapitalMap {
    
    
                fmt.Println(country, "首都是", countryCapitalMap [ country ])
        }

        /*删除元素*/ delete(countryCapitalMap, "France")
        fmt.Println("法国条目被删除")

        fmt.Println("删除元素后地图")

        /*打印地图*/
        for country := range countryCapitalMap {
    
    
                fmt.Println(country, "首都是", countryCapitalMap [ country ])
        }
}

The result of running the above example is:

original map

India 首都是 New delhi
France 首都是 Paris
Italy 首都是 Rome
Japan 首都是 Tokyo
法国条目被删除
删除元素后地图
Italy 首都是 Rome
Japan 首都是 Tokyo
India 首都是 New delhi

Guess you like

Origin blog.csdn.net/u011397981/article/details/131758199