```go
func main() {
f4()
}
func f4() {
m := make(map[string]int)
var lock sync.RWMutex
go func(m map[string]int, l sync.RWMutex) {
for {
time.Sleep(time.Second * 1)
l.RLock()
fmt.Println("token now is : ", m["a"])
l.RUnlock()
}
}(m, lock)
for {
time.Sleep(time.Nanosecond * 1)
lock.Lock()
m["a"] = rand.Int()
lock.Unlock()
}
}
```
讲道理没啥问题呀,怎么还会报读写冲突呢?能解此迷的绝对是牛人。
token now is : 1013340838
token now is : 1876733110
token now is : 166916850
fatal error: concurrent map read and map write
goroutine 5 [running]:
runtime.throw(0x80e8a1a, 0x21)
/usr/local/go/src/runtime/panic.go:608 +0x6a fp=0x842c768 sp=0x842c754 pc=0x806b8fa
runtime.mapaccess1_faststr(0x80d1960, 0x8448120, 0x80e408f, 0x1, 0x0)
/usr/local/go/src/runtime/map_faststr.go:21 +0x34a fp=0x842c7a0 sp=0x842c768 pc=0x8054e4a
main.f4.func1(0x8448120, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0)
/root/go/test/tamp/main.go:31 +0x80 fp=0x842c7d0 sp=0x842c7a0 pc=0x80bee70
runtime.goexit()
/usr/local/go/src/runtime/asm_386.s:1324 +0x1 fp=0x842c7d4 sp=0x842c7d0 pc=0x8091201
created by main.f4
/root/go/test/tamp/main.go:27 +0x62
goroutine 1 [runnable]:
time.Sleep(0x1, 0x0)
/usr/local/go/src/runtime/time.go:100 +0x110
main.f4()
/root/go/test/tamp/main.go:36 +0x76
main.main()
/root/go/test/tamp/main.go:21 +0x17
exit status 2
有疑问加站长微信联系(非本文作者))