Checking Existence
The comma-ok idiom.
Missing Keys Return Zero
When you read a key that does not exist, Go returns the zero value of the value type, not an error. For int that is 0, for string it is the empty string.
package main
import "fmt"
func main() {
m := map[string]int{"a": 5}
fmt.Println(m["missing"])
}The Ambiguity Problem
If a value of 0 is returned, how do you know whether the key exists with value 0, or is simply absent? You cannot tell from the value alone.
All lessons in this course
- Map Internals
- Checking Existence
- Maps as Sets
- Iteration and Ordering