Understand the contract

A data race occurs when goroutines access the same memory concurrently, at least one access is a write and the accesses are not properly synchronized. Go’s race detector instruments execution to report races it observes. Use it with realistic tests and inputs; it cannot inspect paths that never run.

go test -race ./... runs tests with detection enabled on supported platforms and toolchains. Reports show conflicting accesses and the goroutine creation paths. Read both sides of the report, identify the shared object and choose ownership, a mutex or another synchronization rule that covers every relevant access.

Work through the example

The commands below belong at the root of your practice module. Run the mutex example from the previous lesson with go run -race . in its directory. A correct result such as 400 alone does not prove synchronization: an unsafe program may happen to print that value while still containing a race.

go test -race ./...
# In the directory containing the counter program:
go run -race .

Watch this boundary

A race-free program can still deadlock, leak goroutines or violate an application rule. The detector also adds overhead, so do not present its timings as ordinary performance measurements. If the local toolchain cannot support -race, report the limitation and run it in a suitable environment rather than claiming a clean result.

Practice with a changed input

In a disposable copy of the mutex example, remove the locks and run the detector. Restore the locks, repeat the same workload and keep a regression test for the shared-state behavior.

References & further reading

Need help with your Go backend?

I work with engineering teams on focused Go and Kubernetes platform engagements, with limited consulting availability.

Explore Go consulting services