🐹 Go (Golang) Intermediate

What does the -race flag do in go test?

Answer

go test -race runs your tests with Go's built-in race detector enabled. The race detector instruments memory accesses at compile time and dynamically detects when two goroutines concurrently access the same memory location without synchronization, printing a detailed report with goroutine stack traces. It adds roughly 5–10x runtime overhead and 2x memory usage, so it is not used in production but is invaluable in CI pipelines. Always run -race on concurrent code — it catches subtle data races that would otherwise cause intermittent, hard-to-reproduce bugs in production.