Go does not raise errors. Go reports errors.
A common use of a map element zero value:
package main
import (
"fmt"
)
func main() {
counts := map[string]int{}
fmt.Println(counts)
// Equivalent:
// counts["foo"] = counts["foo"] + 1
// counts["foo"] += 1
// counts["foo"]++
counts["foo"]++
fmt.Println(counts)
counts["foo"]++
fmt.Println(counts)
}
Playground: https://play.golang.org/p/nvHBrgV_lFU
Output:
map[]
map[foo:1]
map[foo:2]
A common use of the comma, ok form map index expression:
package main
import (
"fmt"
)
func main() {
m := map[string]map[string]int{}
fmt.Println(m)
// panic: assignment to entry in nil map
// m["K1"]["K2"]++
_, ok := m["K1"]
if !ok {
m["K1"] = map[string]int{}
}
m["K1"]["K2"]++
fmt.Println(m)
m["K1"]["K2"]++
fmt.Println(m)
}
Playground: https://play.golang.org/p/9byxSSIWBre
Output:
map[]
map[K1:map[K2:1]]
map[K1:map[K2:2]]