#### *疑问*
> 为什么 main 方法中循环打印 myMap 时还要加锁?不加锁 go build -race cmd/main.go 编译运行时会提示有两个数据存在竞争关系
```go
package main
import (
"fmt"
"sync"
"time"
)
var (
myMap = make(map[int]int, 10)
lock sync.Mutex
)
func test(n int) {
res := 1
for i := 1; i <= n; i++ {
res *= i
}
lock.Lock()
defer lock.Unlock()
myMap[n] = res
}
func main() {
for i := 1; i <= 20; i++ {
go test(i)
}
time.Sleep(time.Second * 10)
//lock.Lock()
for i, v := range myMap {
fmt.Printf("map[%d] = %d \n \n", i, v)
}
//lock.Unlock()
}
```
有疑问加站长微信联系(非本文作者))