Last active
November 12, 2015 17:14
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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