Data Races and sync.Map
Learn about data races and sync.Map in Go.
We'll cover the following
Data races
A regular Go map
isn’t safe for concurrent access. Maps are often
used to share data between goroutines, but access to a map
must
be synchronized through sync.Mutex, sync.RWMutex
, some other
memory barrier, or coordinated with Go channels to prevent
concurrent access, with the following exception:
Note: map access is unsafe only when updates are occurring. As long as all goroutines are only reading—looking up elements in the map, including iterating through it using a for … range loop—and not changing the map by assigning to elements or doing deletions, it is safe for them to access the map concurrently without synchronization. See the documentation:
Get hands-on with 1400+ tech skills courses.