What Is a Data Race vs a Race Condition in Go

A data race is a specific type of race condition involving unsynchronized concurrent memory access, detectable in Go using the -race flag.

A race condition is a logical bug where program behavior depends on the unpredictable timing of threads, while a data race is a specific type of race condition where two goroutines access the same memory location concurrently and at least one access is a write. Use the Go race detector to find data races by running your code with the -race flag.

go run -race main.go

The detector will report the exact lines where concurrent reads and writes to the same variable occur without synchronization.