Data race is when one concurrent operation attempts to read a variable while at some undetermined time another concurrent operation is attempting to write to the same variable.
occurs when two goroutines access the same variable concurrently and at least one of the accesses is a write
funcmain() { ch :=make(chanint)gofunc() { n :=0// A local variable is only visible to one goroutine. n++ ch <- n // The data leaves one goroutine... }() n :=<-ch // ...and arrives safely in another. n++ fmt.Println(n) // Output: 2}
Race conditionoccurs when two or more operations must execute in the correct order, but the program has not been written so that this order is guaranteed to be maintained.