Skip to content

Day 011 — Maps & Sets, comma-ok, Ordered Iteration

Month 1 · Week 2 · ⬅ Day 010 · Day 012 ➡ · Journal index

🎯 Learning Objective

Use maps idiomatically: distinguish missing keys with comma-ok, model sets, and iterate deterministically.

📚 Topics

  • Map literals, make, delete
  • comma-ok · nil-map write trap
  • Sets with map[T]struct{} · ordered iteration

📖 Reading / Sources

📝 Notes

  • Reading a missing key returns the value type's zero value, not an error. Use the comma-ok form v, ok := m[k] to tell "absent" from "present-but-zero" → [[comma-ok]].
  • The zero value of a map is nil. Reading a nil map is fine; writing to a nil map panics. Always make or use a literal before assigning → [[nil-map-trap]].
  • delete(m, k) is a safe no-op when k is absent — it never panics.
  • Map keys must be comparable (no slices, maps, or functions as keys). Structs of comparable fields are fine.
  • Set idiom: map[T]struct{}struct{}{} values take zero bytes; membership is _, ok := set[k].
  • Iteration order is randomized by the runtime on purpose. For stable output, collect keys into a slice and sort them → [[ordered-iteration]].
  • A map value is not addressable, so you can't do m[k].Field = x for struct values — reassign the whole value, or store a pointer.

💻 Code Examples

// Frequency count: missing key reads as 0, then increments.
freq := make(map[string]int)
for _, w := range words {
    freq[w]++
}
// Deterministic iteration:
keys := make([]string, 0, len(freq))
for k := range freq {
    keys = append(keys, k)
}
sort.Strings(keys)

Full code: examples/month-01/maps-sets/main.go · Run: go run ./examples/month-01/maps-sets

🏋️ Exercises / Practice

Exercise Status Link
Word frequency + Top-N exercises/month-01/week-2/wordfreq
Dedup via set exercises/month-01/week-2/dedup

🐛 Mistakes Made

  • Wrote to a var m map[string]int (nil) → assignment to entry in nil map panic. Added make.
  • Relied on map range order in a test → flaky. Sorted keys for determinism.

❓ Open Questions

  • Are concurrent map reads/writes safe? (No — they race; use a mutex or sync.Map. Week on concurrency.)

🧠 Active Recall (answer without looking)

  1. Q: What does v, ok := m[k] give when k is absent?
    A

v is the value type's zero value and ok is false. 2. Q: Why is map iteration order randomized, and how do you get stable output?

A

It's randomized intentionally so code doesn't depend on it; collect keys into a slice and sort them.

🪶 Feynman Reflection

A map is a labeled drawer system. Asking for an empty drawer hands you a blank (the zero value), so I use comma-ok to ask "is there actually a drawer here?". A set is just drawers where I only care whether the drawer exists, so I store nothing (struct{}{}) inside.

🕳️ Knowledge Gaps

  • sync.Map vs mutex-guarded map trade-offs — defer to concurrency.

✅ Summary

I can use comma-ok, avoid the nil-map write trap, build sets with map[T]struct{}, and iterate maps deterministically by sorting keys.

⏭️ Next Steps / Prep for Tomorrow

  • Day 012: strings, runes, bytes, and UTF-8.

Time spent Difficulty Confidence
90 min 🟦🟦⬜⬜⬜ 🟦🟦🟦🟦⬜

Suggested commit: feat(examples): maps, sets, and ordered iteration (day 011)