[TOC] ## 竞争条件检测 一个没有竞争的 demo ``` func main() { var a int a = 3 fmt.Println("a is ", a) time.Sleep(2 * time.Second) } ``` ``` > go run -race test.go a is 3 ``` 有竞争条件的 demo ``` var a int go func() { a = 3 }() fmt.Println("a is ", a) time.Sleep(2 * time.Second) ``` ``` > go run -race test.go a is 0 ================== WARNING: DATA RACE Write at 0x00c00006c068 by goroutine 7: main.main.func1() D:/go/demo/demo1/a.go:13 +0x3f Previous read at 0x00c00006c068 by main goroutine: main.main() D:/go/demo/demo1/a.go:15 +0x8f Goroutine 7 (running) created at: main.main() D:/go/demo/demo1/a.go:12 +0x81 ================== Found 1 data race(s) exit status 66 ``` 这个命令输出了Warning,告诉我们,goroutine5运行到第11行和main goroutine运行到13行的时候触发竞争了。 而且goroutine5是在第12行的时候产生的。