juniorGolang
How do maps work in Go?
Updated Apr 28, 2026
Short answer
Maps are unordered collections of key-value pairs, providing fast lookups, additions, and deletions.
Deep explanation
Maps are implemented as hash tables. The key must be a comparable type (e.g., strings, ints, but not slices). Reading a missing key returns the zero value of the value type. You can use a two-value assignment to check if a key actually exists.
Real-world example
Caching user session tokens in memory for fast O(1) authorization checks in an API middleware.
Common mistakes
- Reading from and writing to a map simultaneously from multiple goroutines. Standard maps are not thread-safe and will cause a fatal panic.
Follow-up questions
- How do you make a map thread-safe?