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).
Resources
Section titled “Resources”Exercises
Section titled “Exercises”maps1 compile
Section titled “maps1 compile”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_typesmaps2 compile
Section titled “maps2 compile”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.maps3 test
Section titled “maps3 test”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.