What is a race condition?

In short
A race condition happens when two or more parts of a program access the same data at the same time, and at least one of them changes it. The result then depends on timing, so the program may work sometimes and fail other times. In Go, the built-in race detector can find these problems while your program runs.
The core idea
A race condition is a bug where the outcome of a program depends on the order in which concurrent parts run. In Go these parts are goroutines, which are lightweight functions that run alongside other code. A data race is the most common form. It happens when two goroutines access the same variable at the same time and at least one of those accesses is a write. Reads alone are safe. Once a write is involved, the order of operations is no longer guaranteed, and the value you see can be stale, torn, or simply wrong.
A concrete case is a map written by one goroutine while another goroutine writes to it too. Go maps are not safe for concurrent writes, so the program can crash or corrupt its internal state. The bug may not show up on every run, which is why these problems are hard to reproduce.
How the race detector helps
Go ships a tool called the race detector. You turn it on by adding the -race flag to common go commands, such as go test -race, go run -race, go build -race, and go install -race. The detector watches memory accesses while the program runs and prints a report when it sees two conflicting accesses that are not ordered by synchronization.
The report includes stack traces for both conflicting accesses and for the places where the involved goroutines were created. That is usually enough to find the line that needs fixing. Keep in mind that the detector only finds races on code paths that actually run. If your tests cover little, run a binary built with -race under a realistic workload to find more.
The detector has a few tuning options set through the GORACE environment variable. For example, log_path writes reports to a file, and halt_on_error stops the program after the first race. Its exitcode option sets the exit status used after a race is found. Check the documentation for the full list and defaults.
Typical patterns
Most race conditions follow a few shapes. One common case is a loop variable captured by a goroutine. Because the closure shares the loop variable, the goroutine may read it after the loop has changed it. The fix is to pass the value as an argument, so each goroutine gets its own copy.
Another case is an error variable shared between goroutines. Using := inside each goroutine creates a new variable instead of reusing one. A third case is a package-level map or slice touched from several goroutines. Protect it with a sync.Mutex, locking before reads and writes and unlocking afterward.
Even a simple bool or int64 can race. A value written by one goroutine and read by another without synchronization can produce surprising results, because the compiler and processor may reorder operations. A channel, a mutex, or the sync/atomic package can fix this. Atomic operations keep the code lock-free while still making access safe.
Channels have their own pitfall. Sending on a channel and closing it at the same time, without coordination, is a race. A receive that waits for the send to finish before the close makes the order clear.
Costs to know about
The race detector is a debugging aid, not something to leave on in production. The documentation notes that memory use can rise around 5 to 10 times and execution time can rise around 2 to 20 times for a typical program. It also requires cgo to be enabled, and on most systems a C compiler must be installed. Use it in tests and staging runs rather than in release builds.
Alternatives and related ideas
You do not always need shared memory. Go encourages passing data between goroutines through channels, so that only one goroutine owns a value at a time. Designs that avoid sharing avoid whole classes of races. When sharing is unavoidable, a mutex is the clearest tool for protecting a struct or map. Atomic operations suit simple counters and flags. Choose the simplest option that makes the ordering explicit.
FAQ
Is every race condition a data race?
No. A data race is one specific kind of race condition, where two goroutines access the same memory with at least one write and no synchronization. Some race conditions come from logic that depends on timing, even when no memory is shared unsafely. The race detector finds data races but not every timing bug.
Does the race detector slow down my production program?
Yes, it adds significant memory and time overhead. The documentation describes memory use rising several times and execution time rising by a large factor for typical programs. It is meant for testing and debugging, so build your release binaries without the -race flag.
Why does my program print the same value for every goroutine in a loop?
Goroutines started inside a loop often capture the same loop variable. By the time they run, the loop may have finished and the variable has its final value. Passing the value as a function argument gives each goroutine its own copy.
Can I fix a race with a mutex or with atomic operations?
Both can work. A mutex protects a larger section of code or a complex structure such as a map. Atomic operations from the sync/atomic package suit single values like counters or flags. Pick a mutex when several fields must change together.
Sources
This page is based on the official documentation below. Read it for full detail.
Race condition — official documentation ↗