A zero value is the default value of a Go type. It matters when a map key is absent: a lookup of map[int]string returns "" for the missing value. Use the second, Boolean result when you need to distinguish “not found” from a stored empty string. section 3.2.1
Check before using a greeting
With `greeting, ok := phrasebook[l]`, an unsupported language gives `greeting` the string zero value, "", and sets `ok` to false. Check `ok` before returning the greeting; the empty string alone cannot tell you whether the key exists. section 2.3.1
Listing 3.12 bookworm.go: The booksCount() function
// booksCount registers all the books and their occurrences
// from the bookworms shelves.
func booksCount(bookworms []Bookworm) map[Book]uint {
count := make(map[Book]uint) #1
for _, bookworm := range bookworms { #2
for _, book := range bookworm.Books {
count[book]++ #3
}
}
return count
}
In `booksCount`, `count[book]++` starts a missing book’s `uint` count from its zero value. The nested loops can therefore count occurrences without inserting each book first. section 3.2.1
A useful lookup default does not make every zero-valued object ready for every operation. A `Cache[K, V]` whose map has not been initialized cannot safely write entries. Its `New` constructor calls `make(map[K]V)` so the cache is ready to use; `Read` returns both the value and whether its key was found. section 7.1.3
Defaults can also be part of an API’s boundary behavior. `RGBAAt` returns a zero value for a position outside the image, so maze-neighbor coordinates need not be checked before that particular lookup. Separately, `findEntrance` returns `image.Point{}` with an error when no entrance is found; callers check the error. section 9.3.1




