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 or more goroutines access the same variable concurrently and at least one of them changes the value of the variable in some way.