Code-Memo

Safely accessing map keys

Comma-ok idiom
v, ok := m[key]
if !ok {
	// key missing
}
Zero value ambiguity

If V is a type whose zero value is valid (e.g. int with 0), you cannot tell “missing” from “present with zero” without ok or a pointer value type.

maps package (Go 1.21+)
import "maps"

maps.Copy(dst, src)
maps.Equal(m1, m2)
maps.Clear(m)
Concurrent maps

Either protect with sync.Mutex, use sync.Map for specialized caches, or design a single goroutine owner.

Nested maps
outer := map[string]map[string]int{}
inner := outer["a"]
if inner == nil {
	inner = map[string]int{}
	outer["a"] = inner
}
inner["b"] = 1

Or use composite key structs / map[[2]string]int patterns when clearer.