Go maps

A map stores values indexed by keys, allowing efficient lookup, insertion, and deletion. A map must be initialized before writing to it, and looking up a key can return a second boolean indicating whether that key exists.

Minimal example

go
func main() {
    counts := map[string]int{"user-42": 3}
    count, ok := counts["user-42"]
    fmt.Println(count, ok)
}

Learn this concept

Lesson 1.5 · Structs, pointers, slices and maps

Used in exercises

Exercise 1.8 · Practice: write your first Go program Exercise 1.11 · Token bucket limiter with lazy refill Exercise 1.14 · Limiter interface and a FixedWindow implementation Exercise 1.22 · Redis fixed-window limiter that fails open Exercise 1.26 · Optional: Sliding window log limiter Exercise 1.27 · Optional: Sliding window counter limiter Exercise 1.28 · Optional: Leaky bucket limiter Exercise 5.16 · Topic subscription registry
Prerequisite conceptsGo slicesGo structs