Unsynchronized concurrent access to any variable from several goroutines, where at least one of them is undefined behavior by Go memory model .
Undefined means that it says: undefined. Perhaps your program will work correctly, it may work incorrectly. This can lead to memory loss and the type of security provided by Go runtime (see Example below). It can even crash your program. Or it can even lead to an explosion of the Earth (the probability that it is extremely small, maybe even less than 1e-40, but still ...).
This undefined in your case means that yes, i can be nil , partially assigned, invalid, undefined, ... nothing but a or b . This list is just a tiny subset of all the possible results.
Stop thinking that some data races (or may be) are benign or unhealthy. They can be a source of worse things if left unattended.
Since your code is written to the variable a in one goroutine and reads it in another goroutine (which tries to assign its value to another variable i ), this is a data race and, as such, it is unsafe. It doesn’t matter if your tests work "correctly." You can take your code as a starting point, expand / build on it and lead to disaster due to the initially “unhealthy” data race.
For related questions, read How Safe Are Golan Cards for Parallel Read / Write? and Wrong sync in go lang .
It is highly recommended that you read Dmitry Vyukov's blog post: Benign data: what could go wrong?
Also a very interesting blog post showing an example that violates Go memory security with intentional data arrangement: Golan race data for memory security breach
source share