To find a data race in Go, add the -race flag: go run -race main.go, go test -race ./... or go build -race. The go race detector instruments memory accesses at compile time and prints a report with both conflicting stacks whenever two goroutines touch the same variable without synchronisation and at least one access is a write.
It is the most effective tool Go gives you for concurrency bugs, and it is built into the toolchain. This guide shows a real report, how to read it, how to fix the race, and what the detector cannot do.
A data race happens when two goroutines access the same memory location at the same time, at least one access is a write, and nothing orders them (no mutex, channel operation or atomic). The Go memory model leaves the outcome undefined in practice: you can see lost updates, torn values or crashes that only appear under load.
A race is different from a race condition in the general sense. A program can be free of data races and still have wrong ordering logic, and the detector will not flag that.
This counter is incremented by 100 goroutines with no protection.
Without the flag the program often prints a number below 100, or sometimes exactly 100, which is why races are hard to catch by eye. Run it with the detector.
The output looks like this (addresses and line numbers vary).
Read it in three steps:
A program that hits a race exits with status 66 by default, and go test marks the test as failed.
You have three standard options. Pick the one that matches the data.
| Fix | Use when |
|---|---|
sync/atomic types | one integer, pointer or flag |
sync.Mutex or sync.RWMutex | several fields must change together |
| Channels (one owner goroutine) | the data has a clear owner and you pass messages to it |
Run with -race again and the report disappears. Also check the loop variable: since Go 1.22 each iteration has its own variable, which removes a classic source of races in older code that captured i.
Run the whole suite with the detector on every pull request.
Useful settings are passed through the GORACE environment variable.
halt_on_error=1 stops at the first report, and log_path writes reports to files named with the process id. Add -count=10 or -cpu=1,2,4 to vary scheduling and raise the chance of exposing a race.
CGO_ENABLED=1 and a C toolchain must be available. Supported platforms include linux/amd64, linux/arm64, darwin and windows/amd64.You can, but it is slower and larger. Use race builds for tests, load tests and optional canary instances, and ship the normal build.
The detector sees only executed code. If the racing paths never ran concurrently during that run, there is nothing to report, so run more tests, use -count, and exercise concurrent paths.
No. The Go memory model does not promise safe unsynchronised access, and the compiler may reorder or cache values. Always synchronise.
There is no supported per-function switch for application code. Fix the race, or use a build constraint to exclude code from race builds only when you have a proven reason.
Add -race to go run, go test and go build to find data races. Read the report from the top: the conflicting access, the previous access, and the goroutine creation sites. Fix with atomics, mutexes or channels, run the detector in CI on every change, and remember it only catches races that actually run.