Home  >  Article  >  Backend Development  >  Can the length of map be defined in Go language?

Can the length of map be defined in Go language?

青灯夜游
青灯夜游Original
2021-06-04 17:45:376281browse

In the go language, map can define the length. The map can dynamically expand and contract according to the newly added key-value, so it does not have a fixed length or maximum limit, but you can also choose to indicate the initial capacity cap of the map, using the syntax "make(map[keytype]valuetype, cap)".

Can the length of map be defined in Go language?

The operating environment of this tutorial: Windows 10 system, GO 1.11.2, 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.

map This data structure is also called dictionary (Python), hash, HashTable, etc. in other programming languages.

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 it can also Select the initial capacity capacity to indicate the map. 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 Add 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, which maps 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}

Recommended learning: Golang tutorial

The above is the detailed content of Can the length of map be defined in Go language?. 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