From charlesreid1

Line 26: Line 26:


The make function allocates space in memory for the hash table. It returns a map value pointing to this location in memory. It is important to note that the actual implementation of a map is not specified by the language, so it depends on the implementation of the Go runtime being used.
The make function allocates space in memory for the hash table. It returns a map value pointing to this location in memory. It is important to note that the actual implementation of a map is not specified by the language, so it depends on the implementation of the Go runtime being used.
To initialize with literal data, use brackets:
<pre>
commits := map[string]int{
  "asdf": 10,
  "qwerty": 20,
}
</pre>
This also gives a shorter way of creating maps than the make function:
<pre>
commits := map[string]int{}
</pre>


==Working with maps==
==Working with maps==

Revision as of 16:03, 13 December 2018

Go maps are data structures that use a hash table under the hood to provide fast, O(1) lookup of key-value pairs.

Basics

Initialization

To declare a map type, use the notation:

map[KeyType]ValueType

Example: declare a variable that is a map of string keys to integer values:

var m map[string]int

Like pointers/slices, map types are reference types, so if they are not initialized with a value, they have a nil value. A nil map has not been initialized and so has no underlying space in memory, so you cannot write to a nil map.

To initialize space for a map:

m = make(map[string]int)

The make function allocates space in memory for the hash table. It returns a map value pointing to this location in memory. It is important to note that the actual implementation of a map is not specified by the language, so it depends on the implementation of the Go runtime being used.

To initialize with literal data, use brackets:

commits := map[string]int{
  "asdf": 10,
  "qwerty": 20,
}

This also gives a shorter way of creating maps than the make function:

commits := map[string]int{}

Working with maps

To set a key value pair:

m["route"] = 66

To look up a key and assign the value to a variable:

i := m["route"]

Unlike Python, if a key does not exist, Go will return the value type's zero value. For integers, the value is 0; for strings, empty string; for booleans, False; etc.

j := m["root"]
// j == 0

Two useful built-in functions are length (len) and delete (delete):

// number of items in a map
n := len(m)

// remove the specified key from the map
delete(m, "route")

Like Pyhton, Go supports the two-value assignment format:

i, ok := m["route"]

i is the value in the map if the key exists (zero value otherwise), ok is a boolean indicating whether the key is in the map or not.

Also like Python, you can use underscore to skip assignment of one of the multi-assignment values. To check for existence of a key:

_, ok := m["root"]

Iteration

To iterate over a map, use the range keyword, like with lists:

for key, value := range m {
    fmt.Println("Key:", key, "Value:", value)
}

Flags