您有竞争条件,请使用 -race 标志运行您的程序go run -race main.go==================WARNING: DATA RACERead at 0x0000005e9600 by main goroutine: main.main() /home/jack/Project/GoProject/src/gitlab.com/hooshyar/GoNetworkLab/StackOVerflow/race/main.go:17 +0x6cPrevious write at 0x0000005e9600 by goroutine 6: main.main.func1() /home/jack/Project/GoProject/src/gitlab.com/hooshyar/GoNetworkLab/StackOVerflow/race/main.go:13 +0x56Goroutine 6 (running) created at: main.main() /home/jack/Project/GoProject/src/gitlab.com/hooshyar/GoNetworkLab/StackOVerflow/race/main.go:11 +0x46==================result=119657339Found 1 data race(s)exit status 66什么是解决方案?有一些解决方案,一个解决方案是使用互斥体:var a = 0func main() { var mu sync.Mutex go func() { for { mu.Lock() a = a + 1 mu.Unlock() } }() time.Sleep(3*time.Second) mu.Lock() fmt.Printf("result=%d\n", a) mu.Unlock()}在任何读写锁定互斥体然后解锁它之前,现在您没有任何竞争,并且结果将在最后为 big int 。