Best Division

Spread Your Knowledge

  • All topics
  • About us
  • Contact us
  • Privacy policy
  • Terms of service
  • Copyright
  • Facebook
  • Twitter
  • Instagram
  • Youtube
  • LinkedIn
  • Pinterest
  • Github
  • Tumblr

© 2026 Best Division

How Do You Find Data Races with go run -race?

GoSoftware Testing
Jayant Kumar
Jayant Kumar@jayantkumar314
PublishedDec 6, 2026
6 min read

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.

Table of Contents

  • What a data race is
  • Running go run -race on a racy program
  • Reading a race report
  • Fixing the race
  • Using the detector in tests and CI
  • Limits and cost of the detector
  • Frequently Asked Questions
    • Does go build -race produce a binary I can ship?
    • Why does the race detector not find my race?
    • Is a race on an int harmless on 64-bit machines?
    • Can I disable the detector for one function?
  • Summary
  • Related Articles

What a data race is

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.

Running go run -race on a racy program

This counter is incremented by 100 goroutines with no protection.

go

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.

bash

Reading a race report

The output looks like this (addresses and line numbers vary).

text

Read it in three steps:

  1. The first block is the access that triggered the report, with the goroutine id and the exact source line.
  2. The "Previous" block is the earlier conflicting access. The two lines together tell you which variable and which code paths collide.
  3. The "created at" blocks show where each goroutine was started, which helps when many goroutines run the same function.

A program that hits a race exits with status 66 by default, and go test marks the test as failed.

Fixing the race

You have three standard options. Pick the one that matches the data.

go
FixUse when
sync/atomic typesone integer, pointer or flag
sync.Mutex or sync.RWMutexseveral 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.

Using the detector in tests and CI

Run the whole suite with the detector on every pull request.

bash

Useful settings are passed through the GORACE environment variable.

bash

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.

Limits and cost of the detector

  • It only reports races that actually happen during that run. Code paths your tests never execute are never checked, so coverage matters.
  • It slows programs and uses more memory. The Go documentation gives typical figures of 5 to 10 times the memory and 2 to 20 times the execution time, so use it in tests, staging and canary builds rather than in normal production binaries.
  • It needs cgo on most platforms, so CGO_ENABLED=1 and a C toolchain must be available. Supported platforms include linux/amd64, linux/arm64, darwin and windows/amd64.
  • It does not find deadlocks, goroutine leaks or logic races. Those need other tools.

Frequently Asked Questions

Does go build -race produce a binary I can ship?

You can, but it is slower and larger. Use race builds for tests, load tests and optional canary instances, and ship the normal build.

Why does the race detector not find my race?

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.

Is a race on an int harmless on 64-bit machines?

No. The Go memory model does not promise safe unsynchronised access, and the compiler may reorder or cache values. Always synchronise.

Can I disable the detector for one function?

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.

Summary

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.

Related Articles

  • How Does Go Handle Deadlocks and Race Conditions?
  • How Does the Go Scheduler (GMP Model) Work?
  • Learn Go: Complete Study Roadmap from Beginner to Advanced
Share

Similar posts

  • How Does sync.Once Work in Go?
  • When Should You Use sync.RWMutex in Go?
  • What Happens When You Use a Nil Channel in Go?
  • What Are Send-Only and Receive-Only Channels in Go?
  • How Do You Range Over a Channel and Detect Closure in Go?
  • How Do You Version and Publish a Go Module?
  • How Does the internal Package Rule Work in Go?
  • What Is the Standard Go Project Layout (cmd, internal, pkg)?