Skip to content

Maps

Beginner · Collections & Loops

A map is an unordered collection of key/value pairs: map[K]V. Create with make or a literal, read with m[k], and use the comma-ok form v, ok := m[k] to tell a missing key from a zero value. Delete with delete(m, k).

Declare a map’s key and value types.

Show hint
Maps have types for both keys and values.
Define these types to make the code compile.
Indexing in maps is similar to setting values.
Check the spec for more info: https://go.dev/ref/spec#Map_types

Source

Initialize a map with a literal.

Show hint
Similar to `maps1`, but you can initialize a new map in the same line using a different syntax.

Source

Look up the correct key in a map.

Show hint
Verify that the keys used to access map values match the keys defined in the phoneBook map.

Source