Overview
Count how often each HTTP status code appears in a Go server log with a map, and avoid the panic of writing to a nil map.
- a missing key reads as zero
- m[k]++ counts
- writing to a nil map panics
Short Course
A missing map key reads as the zero value, so m[k]++ just works. Trap:
var m map[K]V is a nil map, and writing to it panics. Create maps with
make or {}.
hits := map[string]int{}
hits["/home"]++
hits["/cart"] // 0: missing reads as zero
var bad map[string]int
bad["/"] = 1 // panic: nil map
Task
Write CountStatus(codes []int) map[int]int: how many times each status code
appears. Example: CountStatus([]int{200, 404, 200}) → map[200:2 404:1]. Always return a map you can write to, even for an empty log.
Test Suites
Pending
Next
Output