Detecting race conditions
If you use the -race flag when running or building a Go program, you will turn on the Go race detector, which makes the compiler create a modified version of the typical executable file. This modified version can record the accesses to shared variables as well as all synchronization events that take place, including calls to sync.Mutex, sync.WaitGroup, and so on. After doing some analysis of the events, the race detector prints a report that can help you identify potential problems so that you can correct them.
In order to showcase the operation of the race detector, we will use the code of the rd.go program, which will be presented in four parts. For this particular program, the data race will happen because two ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access