Skip to content

Instantly share code, notes, and snippets.

@Deleplace
Last active August 1, 2018 07:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Deleplace/0bfbf70427c1ea84cff5dc8742ca1ae1 to your computer and use it in GitHub Desktop.
Save Deleplace/0bfbf70427c1ea84cff5dc8742ca1ae1 to your computer and use it in GitHub Desktop.
The Go race detector catches synchronization bugs only when they actually occur.
package main
import (
"fmt"
"math/rand"
"sync"
"time"
)
func main() {
// Seeding is important for choosing a random counter
rand.Seed(time.Now().Unix())
// 3 counters
a := []int{0, 0, 0}
// 2 tasks will be launched. The main goroutine will
// wait for them to complete.
var wg sync.WaitGroup
wg.Add(2)
go func() {
i := rand.Intn(3)
a[i]++
wg.Done()
}()
go func() {
j := rand.Intn(3)
a[j]++
wg.Done()
}()
wg.Wait()
fmt.Println(a)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment