Skip to content

Instantly share code, notes, and snippets.

@skatsuta
Last active November 12, 2015 17:14
Show Gist options
  • Save skatsuta/56318a3f208f88bf20bd to your computer and use it in GitHub Desktop.
Save skatsuta/56318a3f208f88bf20bd to your computer and use it in GitHub Desktop.
Race condition demo of a map in Go. Try `go run race_map.go` many times.
package main
import (
"sync"
"github.com/k0kubun/pp"
)
func main() {
m := map[int]string{}
a := 97
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
m[i] = string(a + i)
wg.Done()
}()
}
wg.Wait()
pp.Printf("%v\n", m)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment