When two or more goroutines have unsynchronized access to a shared resource and attempt to read and write to that resource at the same time, you have what’s called a race condition.

    example program that contains a race condition

    1. var (
    2. counter int
    3. wg sync.WaitGroup
    4. )
    5. func main() {
    6. wg.Add(2)
    7. go incCounter(1)
    8. go incCounter(2)
    9. wg.Wait()
    10. fmt.Println("Final Counter:", counter)
    11. }
    12. func incCounter(id int) {
    13. defer wg.Done()
    14. for count := 0; count < 2; count++ {
    15. value := counter
    16. runtime.Gosched()
    17. value++
    18. counter = value
    19. }
    20. }

    Build the code using the race detector flag
    go build -race