Go Maps: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 9: Line 9:
A map is an unordered collection of key/value pairs. The map implementation uses the key as an index and retrieves the associated value in O(1).
A map is an unordered collection of key/value pairs. The map implementation uses the key as an index and retrieves the associated value in O(1).


The key can be of any type for which the equality operator <code>[[Go_Language#Equality_Operator|==]]</code> is defined: integer, floating point and complex number, string, pointer, interface (as long as the dynamic type supports equality), structs and arrays. [[Go_Slice|Slices]] cannot be used as map keys because equality is not defined on them.
The key can be of any type for which the equality operator <code>[[Go_Language#Equality_Operator|==]]</code> is defined: integer, floating point and complex number, string, pointer, interface (as long as the dynamic type supports equality), structs and arrays. [[Go_Slices#Overview|Slices]] cannot be used as map keys because equality is not defined on them.


Like slices, maps hold references to the underlying data structures.
Like slices, maps hold references to the underlying data structures.

Revision as of 00:19, 27 August 2024

External

Internal

Overview

A map is an unordered collection of key/value pairs. The map implementation uses the key as an index and retrieves the associated value in O(1).

The key can be of any type for which the equality operator == is defined: integer, floating point and complex number, string, pointer, interface (as long as the dynamic type supports equality), structs and arrays. Slices cannot be used as map keys because equality is not defined on them.

Like slices, maps hold references to the underlying data structures.

Maps and Pass-by-Value

Pointers to Maps

nil and Empty Map

nil Map

Empty Map

Declaration and Initialization

A map can be declared with the long variable declaration.

var <mapVarName> map[<keyType>]<valueType>

var mm [string]int

This declaration creates a nil map. Because nil maps cannot be written, this form has a limited usefulness.

Inside functions, the short variable declaration can be used with make() or a composite literal.

Initialization with make()

make() allocates a the underlying data structures required by the map, and creates a new map header to describe it, all at once. Note that unlike new(), make() returns the map, not a pointer. The returned map is empty, but ready to use.

mm := make([string]int)

Initialization with a Composite Literal

Composite literals can be used for map initialization.

Long variable declaration and initialization with type inference:

var mm = map[string]int{"A": 1, "B": 2}

Short variable declaration:

mm := map[string]int{"A": 1, "B": 2}

Operators

Indexing Operator []

Values stored in a map can be referenced with the indexing operator [], using the [key] syntax.

The indexing operator returns two values: a copy of the value associated with the key, or the zero-value for the map type if the key does not exist in map, and a boolean that says whether the key exists or not in the map.

The second returned value can be syntactically omitted, but given the fact that the indexing operator returns the zero value for the type and not nil on missing keys, it is impossible to say without it whether the key is missing, or a zero-value was associated with the key.

mm := map[string]int{"A": 1, "B": 2}
v, ok := mm["A"]
fmt.Printf("%d, %t\n", v, ok) // will display 1, true

Idiom:

if v, ok := mm["A"]; ok {
  // use the value
}

If the key does not exist in map, the retuned value is not nil, but the zero value for the type.

The indexing operator can be used to change the value associated with a key, or to add a new key/value pair:

mm["A"] = 7
mm["C"] = 9

Map Functions

delete()

delete() is a built-in function that deletes a key/value pair from the map:

mm := map[string]int{"A": 1, "B": 2}
delete(mm, "A")

If the key does not exist in the map, delete() is a no-op.

len()

len() is a built-in function that returns the number of keys in the map. Note that the built-in cap() function does not work on maps. Unlike slices, maps do not have the concept of capacity.

make()

make() is a built-in function that creates ready-to-use map. See Initialization with make() above.

new()

Iterating over a Map

Use the range keyword.

for k, v := range mm {
    fmt.Printf("%s=%d\n", k, v)
}

If we only want to iterate over keys and the values are not needed, this syntax is valid:

for k := range mm {
  fmt.Printf("key: %s\n", k)
}

TODO

Go_Maps_TODEPLETE