SoFunction
Updated on 2025-03-05

Detailed explanation of map type instances of GO language

Introduction to GO language map

The structure of map iskeyandvalueThe form, but it stores itDisorderIt'sQuoteTypes, to a certain extent, maps can actually be classified as arrays, which is equivalent to making some extensions based on arrays to implement certain corresponding functions.

Definition format of GO language map

var mapvariable map[keyTypes of] valueTypes of
mapvariable = map[keyTypes of] valueTypes of{}

example:

var map1 map[int]string
(map1)
map1 = map[int]string{1:"a",2:"b"}
(map1)

The output is:

map[]
map[1:a 2:b]

When map is initialized, if no value is assigned, the default value is`nil` That is, empty value

Map is similar to an array, and can also be used`make`Form to assign value

var map1 = make(map[int]string)
(map1)
map1[1] = "a"
(map1)

Output:

map[]
map[1:a]

usemakeAfter declaration and initialization, you can use an arrayarr[i]The same form as the map value

Make a small example to deepen some influence: give a map format and assign values, and let the map's keys and values ​​exchange positions

package main
import "fmt"
func main(){
    var map1 = map[int]string{1: "a", 2: "b", 3: "c", 4: "d"}
    (map1)
    var map2 = make(map[string]int)
    for k,v := range map1{
        map2[v] =k
    }
    (map2)
}

The output is:

map[2:b 3:c 4:d 1:a]
map[c:3 d:4 a:1 b:2]

Detect whether the map's key-value pair exists

Remember this form when writing variable assignments?

var str = "str"
var str1,_ = str

It's the above`_`,In GO language, two states will be returned, one is the returned value and the other is the state of the value. If the value is true, the following`_`Yes`true`, otherwise it is`false`

package main
import "fmt"
func main(){
    var map2 = make(map[string]int)
    (map2)
    if _, err := map2["a"]; err {
        map2["e"] = 5
    }
    (map2)
}

The output is:

map[b:2 e:5 c:3 d:4 a:1]

In the above example, when`map2["a"]The value of ` is true, give it to`map2`A new value is added, otherwise, it can be determined whether this key exists

Delete a key value in the map

Direct `delete(map1, key1)` That's OK

var map1 = map[int]string{1: "a", 2: "b", 3: "c", 4: "d"}
(map1)
delete(map1, 2)
(map1)

The output is:

map[1:a 2:b 3:c 4:d]
map[1:a 3:c 4:d]

Why don't you need to return the value when deleting a map using `delete`?

As mentioned earlier, map is a reference pass. When deleting, it is equivalent to directly deleting the value of this memory.

Sorting of maps

Since maps are stored in a non-regular way, map sorting does not exist, but in some cases, sorting is required, so use `for` to use `key`value` to sort correspondingly, and then reassign the value again

package main
import (
    "fmt"
    "sort"
)

var (
    barVal = map[string]int{"alpha": 34, "bravo": 56, "charlie": 23, "delta": 87, "echo": 56, "foxtrot": 12, "golf": 34, "hotel": 16, "indio": 87,"juliet": 65, "kili": 43, "lima": 98}
)
func main() {
    for k, v := range barVal {
        ("Key: %v, Value: %v / ", k, v)
    }
     keys := make([]string, len(barVal))
    i := 0
    for k, _ := range barVal {
        keys[i] = k
    i++
    }
    (keys)
    ()
    ("sorted:")
    for _, k := range keys {
        ("Key: %v, Value: %v / ", k, barVal[k])
    }
}

You can run the above string of codes by yourself. For more information about maps, you can check the relevant documents of GO

This article focuses on the map type of GO, including the creation, assignment, sorting, deletion, etc. For more information about maps in GO, please see the relevant links below