To understand -race
flag better, we can refer to the Go documentation:
Data races are among the most common and hardest to debug types of bugs in concurrent systems. A data race occurs when two goroutines access the same variable concurrently and at least one of the accesses is a write.
It is easy to use via the command line interface. So, how to use -race
flag in GoLand with tests? Is it possible to use -race
always in tests? Here you go.
Add -race
flag to a specific test
- Navigate to a test file, click on the gutter icon near the test name and select Modify Run Configuration.
- Add
-race
entry to the Go tool arguments field. - Save changes and run the configuration again.
Add -race
flag to all tests
- Navigate to Help | Find Action in the main menu (Shift+Shift by shortcuts) and type Edit Configurations, press Enter.
- Select the Edit configuration templates option, find Go Test.
- Add
-race
entry to the Go tool arguments field. Pay attention that changing a template does not affect the existing configuration and you should add the flag manually to the same field.
Congratulations! 🎉 Now your tests use -race
flag and you can see warnings from the compiler.
Top comments (2)
Please note that the race detector only finds races that happen at runtime, so it can't find races in code paths that are not executed. If your tests have incomplete coverage, you may find more races by running a binary built with -race under a realistic workload.
golang.org/doc/articles/race_detec...
Good point, thanks for sharing :)