Stage 1 · Code
Functions & Composite Types
Maps
Store and look up key-value pairs in constant time with Go's built-in map type.
Creating Maps
A map is an unordered collection of key-value pairs where every key is unique. Keys must be a comparable type (strings, integers, booleans, structs with comparable fields). Values can be any type.
This is one of the most common runtime panics for Go beginners. If you declare a map with var m map[K]V and then assign m[key] = value, the program panics. Always initialize with m = make(map[K]V) or a map literal before writing.
Reading and Writing
Use m[key] to read a value and m[key] = value to write. Reading a key that does not exist returns the zero value for the value type — it does not panic or produce an error.
The Comma-Ok Idiom
To distinguish between a missing key and a key mapped to the zero value, use the two-value form: value, ok := m[key]. ok is true if the key is present, false if it is not.
Deleting Entries
The built-in delete(m, key) removes a key from a map. Deleting a key that does not exist is safe — it is a no-op. There is no return value.
Iterating with range
Use range to iterate over all key-value pairs in a map. The iteration order is random on every run — Go deliberately randomizes it to prevent programs from accidentally depending on insertion order.
Like slices, maps are passed by reference. Passing a map to a function does not copy the data — the function operates on the same underlying hash table. Modifications inside the function are visible to the caller.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.