How do I detect data races with -race in Go?

Detecting data races in Go can be accomplished by using the built-in race detector. The race detector helps identify concurrent access to shared variables that may lead to inconsistent behavior in your programs.

To use the race detector, you simply need to enable the -race flag when you run your Go application or tests. This will allow the runtime to track memory accesses and report any data races it detects.

Example of Using the -race Flag

Here is a simple example that demonstrates how to run your Go program with the race detector enabled:

go run -race your_program.go

If your program contains data races, you will receive output that includes details about the detected races, allowing you to identify and fix the problematic code.


Go data races -race flag concurrency race detector Go programming