When you don’t use sync.Mutex to ensure exclusive access to data between goroutines or forget to lock in parts of the program, you’ll get data races.

Data races might lead to memory corruption or crashes.

Go makes it easy to instrument the code with additional checks that are very likely to catch data races.

Use -race flag to go build or go run.

Here’s a program with intentional data races.

When you run it with go run -race data_race.go the runtime will notice memory corruption.

https://codeeval.dev/gist/c5fce4941219e0ec33c4a45bffde6054

This examples shows that memory for variable n is corrupted because the final value of n is not what we expect.

It also shows that instrumentation added with -race can catch memory corruption and points out which part of program caused the corruption.

When to use -race

Additional instrumentation added by -race flag makes the program slower so it’s not usually used when compiling shipping binaries.

It’s a good idea to use on your CI (continous integration) servers when running your test suite.

Race detector and Windows

Race detector requires gcc to be installed on your machine.

gcc is typically installed on Mac OS and Linux but usually not on Windows.

To install gcc on Windows, you can use a window-specific distribution of gcc, like MinGW. Make sure gcc is in PATH.