race detector found a data race in a shared counter
Ran go test -race ./... for the first time in a while and it flagged a race:
WARNING: DATA RACE
Write at 0x00c00009e020 by goroutine 12:
main.(*Counter).Inc()
/app/counter.go:15
Previous read at 0x00c00009e020 by goroutine 11:
main.(*Counter).Value()
/app/counter.go:19
It's a plain int field accessed from multiple goroutines. I know I need synchronization. Is sync/atomic or a mutex the right tool here? Counter is read way more often than written.
4 replies