package main
import("fmt""sync")var x int=0funcAdd(y int){
x = x + y
}funcRace(){var wg sync.WaitGroup
for i :=0; i <1000; i++{
wg.Add(1)gofunc(){defer wg.Done()Add(10)}()}
wg.Wait()
fmt.Println(x)}funcNoRace(){for i :=0; i <1000; i++{Add(10)}
fmt.Println(x)}funcmain(){Race()NoRace()}
$ go test -race mypkg // test the package
$ go run -race mysrc.go // compile and run the program
$ go build -race mycmd // build the command
$ go install -race mypkg // install the package
背景在阅读golang runtime相关代码时,发现有个变量raceenabled,于是查了下文档了解了下具体案例了解raceenabled前,先看这么段代码package mainimport ( "fmt" "sync")var x int = 0func Add(y int) { x = x + y}func Race() { var wg sync.WaitGroup for i := 0; i < 1000; i++ { wg.Add(1