Skip to content

Lesson 7: Maps

Big Picture

flowchart LR
    K["key (hashable)"] -->|hash function| B["bucket"]
    B --> V["value"]
    subgraph "map[string]int"
        B1["'one'"] --> V1["1"]
        B2["'two'"] --> V2["2"]
    end
    Q["v, ok := m['k']"] -->|"ok == false"| Z["zero value,<br/>NOT an error"]

Two idioms to memorize: make(map[K]V) (a nil map panics on write!) and the comma-ok pattern v, ok := m[k] to tell "absent" apart from "zero value".

Concepts

Creating Maps

// Using make
m := make(map[string]int)

// Map literal
m := map[string]int{
    "one": 1,
    "two": 2,
}

Operations

m["key"] = value        // Set
value := m["key"]       // Get
delete(m, "key")        // Delete
len(m)                  // Length

Check Key Exists

value, ok := m["key"]
if ok {
    // key exists
}

Iteration

for key, value := range m {
    // process key, value
}

Code Walkthrough

package main

import "fmt"

func main() {
    ages := map[string]int{"alice": 30}
    ages["bob"] = 25

    v, ok := ages["carol"]          // comma-ok
    fmt.Println(v, ok)              // 0 false

    delete(ages, "alice")
    for k, v := range ages {
        fmt.Println(k, v)
    }
}

Full program: code/07_maps.go.

Try it yourself

cd code
go run . maps

Exercises

  1. Write a word-frequency counter: map[string]int over a slice of strings.
  2. Try writing to a var m map[string]int without make — what happens?
  3. Build a nested map map[string]map[string]int (e.g., city → metric → value).

Check Your Understanding

#

v := m["missing"] on a map[string]int — what is v?

#

How do you check if key "k" exists in map m?