Tag Archives: fatal error: concurrent map read and map writ

Golang Error: fatal error: concurrent map read and map writ

The reason for the error is that the map is thread unsafe and an error will be reported when there are concurrent reads and writes
solution: use the read-write lock sync.rwmutex:
golang read-write lock
rules
when the read lock exists, other read locks can exist together, but the write lock needs to wait
when the write lock exists, both the read lock and the write lock need to wait
the read lock can be concurrent, and the read lock and the write lock are mutually exclusive, Write lock and write lock are mutually exclusive

var rw sync.RWMutex
//Read lock and locking
rw.Rlock()
//Read lock and unlocking
rw.Runlock()