Home  >  Article  >  Backend Development  >  What is map in golang

What is map in golang

青灯夜游
青灯夜游Original
2023-01-13 11:07:133927browse

In golang, map is a special data structure. It is an unordered collection in the form of key (index) and value (value). It can also be called an associative array or dictionary; map is a The ideal structure that can quickly find values ​​is a data structure that can quickly find the corresponding value based on a given key.

What is map in golang

The operating environment of this tutorial: Windows 7 system, GO version 1.18, Dell G3 computer.

Map in Go language is a special data structure, an unordered collection of element pairs (pair). Pair corresponds to a key (index) and a value (value), so this structure is also called Associative array or dictionary is an ideal structure that can quickly find values. Given a key, the corresponding value can be quickly found.

The key of Golang's map can be any data type that can be compared using ==, such as int, string, bool, etc., and the value can be any type.

Map is an unordered data structure, so the same map may be obtained in an inconsistent order each time it is traversed.

map concept

map is a reference type and can be declared in the following way:

var mapname map[keytype]valuetype

where:

  • mapname is the variable name of map.

  • keytype is the key type.

  • valuetype is the value type corresponding to the key.

Tip: Spaces are allowed between [keytype] and valuetype.

You do not need to know the length of the map when declaring it, because the map can grow dynamically. The value of an uninitialized map is nil. You can use the function len() to get the number of pairs in the map.

[Example]

package main
import "fmt"
func main() {
    var mapLit map[string]int
    //var mapCreated map[string]float32
    var mapAssigned map[string]int
    mapLit = map[string]int{"one": 1, "two": 2}
    mapCreated := make(map[string]float32)
    mapAssigned = mapLit
    mapCreated["key1"] = 4.5
    mapCreated["key2"] = 3.14159
    mapAssigned["two"] = 3
    fmt.Printf("Map literal at \"one\" is: %d\n", mapLit["one"])
    fmt.Printf("Map created at \"key2\" is: %f\n", mapCreated["key2"])
    fmt.Printf("Map assigned at \"two\" is: %d\n", mapLit["two"])
    fmt.Printf("Map literal at \"ten\" is: %d\n", mapLit["ten"])
}

Output result:

What is map in golang

In the example, mapLit demonstrates the use of {key1: value1, key2: value2} format to initialize maps, just like arrays and structures.

The creation method of mapCreated in the above codemapCreated := make(map[string]float) is equivalent to mapCreated := map[string]float{} .

mapAssigned is a reference to mapList, and modifications to mapAssigned will also affect the value of mapLit.

Note: You can use make(), but you cannot use new() to construct a map. If you use new() incorrectly to allocate a reference object, you will get a null reference pointer, which is equivalent to declaring a Uninitialized variable and took its address:

mapCreated := new(map[string]float)

Next, when we call mapCreated["key1"] = 4.5, the compiler will report an error:

invalid operation: mapCreated["key1"] (index of type *map[string]float).

map capacity

Different from arrays, map can dynamically expand and contract according to the new key-value, so it does not have a fixed length or maximum limit, but you can also choose to mark the map Initial capacity capacity, the format is as follows:

make(map[keytype]valuetype, cap)

For example:

map2 := make(map[string]float, 100)

When the map grows to the upper capacity limit, if a new key-value is added, the size of the map will automatically increase by 1, so For performance reasons, for large maps or maps that will expand rapidly, even if the capacity is only roughly known, it is best to indicate it first.

Here is a specific example of map, that is, mapping musical scales to corresponding audio:

noteFrequency := map[string]float32 {
"C0": 16.35, "D0": 18.35, "E0": 20.60, "F0": 21.83,
"G0": 24.50, "A0": 27.50, "B0": 30.87, "A4": 440}

[Related recommendations: Go video tutorial, programming teaching

The above is the detailed content of What is map in golang. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn