Skip to main content

Go Maps

A map stores data by connecting keys to values, and every element is one key:value pair.

Elements have no fixed order, you can change them freely, and the same key cannot appear twice.

To find how many elements a map has, which is its length, call len().

A map that has not been initialized has the value nil.

Under the hood, a map holds references to a hash table.

Go gives you several ways to create a map, and the next sections go through them.


Create Maps Using var and :=

The var keyword and the short declaration operator := both work for creating a map with starting values.

Note: Don't expect a map to keep the order you wrote in your code. Go arranges the stored data its own way so that looking values up stays fast.

Example

Prompt
Create two maps using literal syntax: a string-to-string map for a car and a string-to-int map for cities. Print both maps.
Output: Go program and captured output

Syntax: Map With var and :=

main.go
var a = map[KeyType]ValueType{key1:value1, key2:value2,...}
b := map[KeyType]ValueType{key1:value1, key2:value2,...}

Create Maps Using the make() Function

The make() function is another option. It gives you a map that you can then fill one key at a time.

Example

Prompt
Create two maps using the make() function. Add key-value pairs one at a time for a car and cities. Print both maps.
Output: Go program and captured output

Syntax: Map With make()

main.go
var a = make(map[KeyType]ValueType)
b := make(map[KeyType]ValueType)

Create an Empty Map

An empty map can come from the make() function, or from a var declaration with no initial value.

Note: Only the first of these is ready to use, so make() is the one to choose. The var version is nil, as the output shows, and writing to it causes a runtime panic.

Example

Prompt
Show the difference between creating an empty map with make() and without it. Print whether each is nil.
Output: Go program and captured output

Allowed Key Types

A key can use any type that supports the equality operator (==), because Go must be able to compare keys. That covers:

  • Booleans
  • Numbers
  • Strings
  • Arrays
  • Pointers
  • Structs
  • Interfaces (provided the dynamic type can be compared for equality)

Three types cannot be keys:

  • Slices
  • Maps
  • Functions

The reason is that == is not defined for any of them.


Allowed Value Types

Values have no such limit: a map can hold values of any type.


Access Map Elements

To read an element, put its key in square brackets after the map's name.

Example

Prompt
Create a map with make(), add brand, model, and year keys, then access and print the "brand" value.
Output: Go program and captured output

Syntax: Access a Map Element

main.go
value = map_name[key]

Update and Add Map Elements

Assigning a value to a key does two jobs. If the key already exists, its value is updated; if not, a new element is added.

Example

Prompt
Create a map, print it, then update one element and add a new one. Print the map again.
Output: Go program and captured output

Syntax: Update a Map Element

main.go
map_name[key] = value

Remove Element from Map

Call the delete() function with the map and a key to remove that element.

Example

Prompt
Create a map, print it, delete the "year" key using delete(), and print the map again.
Output: Go program and captured output

Syntax: Delete a Map Element

main.go
delete(map_name, key)

Check For Specific Elements in a Map

Looking up a key can return two values, which gives the val, ok pattern. ok tells you whether the key exists, even when its value is empty, as with day in the example.

When the existence of the key is all you care about, put the blank identifier (_) where val would go.

Example

Prompt
Check for existing and non-existing keys in a map using the val, ok pattern. Also use the blank identifier to check only for key existence.
Output: Go program and captured output

Syntax: Check a Map Key

main.go
val, ok := map_name[key]

Maps Are References

A map variable refers to a hash table rather than containing the data itself.

So when two variables point at one hash table, a change made through either variable is visible through the other.

Example

Prompt
Show that maps are references. Assign map a to b, change a value in b, and show that a is also changed.
Output: Go program and captured output

Iterate Over Maps

A range loop walks through every key and value in a map. Don't count on the order, since it can change from one run to the next.

Example

Prompt
Iterate over a map using range and print each key-value pair.
Output: Go program and captured output

Iterate Over Maps in a Specific Order

A map has no order of its own. To visit its elements in a particular sequence, keep that sequence in another data structure, such as a slice of keys, and loop over that instead.

Example

Prompt
Show how to iterate a map in a specific order by using a separate slice that defines the order.
Output: Go program and captured output