Maps are hash tables: map[string]int. Keys must be comparable types. Like Python dicts or Java HashMaps—O(1) average lookup.
Creating and accessing
ages := map[string]int{"Ada": 36}
ages["Grace"] = 89
v, ok := ages["Unknown"] // ok false if missing
Reading a missing key returns zero value without panic—use comma-ok or delete intentionally. Maps are reference types; nil maps cannot be written to until initialized with make.
Iteration
for k, v := range m order is randomized—do not depend on key order for logic.
Important interview questions and answers
- Q: Zero value of map?
A: nil—assignments panic untilmake(map[K]V)or literal. - Q: Concurrent map access?
A: Not safe—usesync.Mapor mutexes; or one goroutine owns the map.
Self-check
- How do you test if a key exists?
- Is map iteration order stable?
Pitfall: Writing to a nil map panics—initialize with make(map[K]V) or a literal before assignment.
Interview prep
- Nil map write?
Panics—initialize with make or literal before assignment.
- Map iteration order?
Randomized—never depend on key order for logic.