Data races occur when multiple goroutines access the same variable and at least one access is a write, leading to non-deterministic behavior. Using a bookstore analogy, the article demonstrates how concurrent access to shared data can cause problems like double-selling inventory. It shows how to detect races using Go's race detector with the `-race` flag, which instruments code to identify conflicting reads and writes. The article walks through a practical example where two customer goroutines compete to buy the last copy of a book, demonstrating both the theoretical definition and practical consequences of data races.

10m read timeFrom bitfieldconsulting.com
Post cover image
Table of contents
A shared variableRacing goroutinesData races in concurrent systemsDouble access problemsClash of the customersDemonstrating the raceThe race detector output

Sort: