Maps
A map is like drawers with labels.
Each label is a key. Inside the drawer is a value.
You open a drawer by its label and get the value fast.
Keys must be types you can compare, like string or int.
Creating a map
Make a map with a literal or with make.
Write map[KeyType]ValueType.
Never write into a nil map. Create it first.
Writing and reading
Set a value with m[key] = value.
Read a value with m[key].
If the key is missing, you get the starter value for that value type.
Checking if a key exists
Use the two-value form: value, ok := m[key].
If ok is false, that drawer was empty. The key was missing.
This stops you from confusing a real 0 with a missing key.
delete
delete removes a key and its value.
It is safe even if the key was already gone.
Ranging over a map
range walks each key and value.
The order is not promised. Do not depend on a fixed order.
Create the map with make or a literal before you write into it.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.