Why a Read-Only Go Map Still Needs Synchronisation
A Go map that nobody writes to is safe for concurrent reads. That sounds simple, until "nobody writes to it" quietly turns into "nobody writes to the map, but the values contain slices, and the map itself is replaced during reloads".
Read-only is a property of the whole reachable object graph, and the map variable is only one part of it. You also need to publish the finished map correctly and decide what callers are allowed to do with the values they get back.
Concurrent reads are fine, if the map is genuinely finished
The built-in Go map supports multiple concurrent readers once the map is no longer being modified. Looking up keys and ranging over the map are both fine in that situation. The Go documentation's warning is about simultaneous reads and writes; readers don't need a lock just because they happen to run in different goroutines.
This is a perfectly reasonable pattern:
func serve(m map[string]string, jobs <-chan string) {
for key := range jobs {
value := m[key]
fmt.Println(value)
}
}
func main() {
messages := map[string]string{
"en": "hello",
"fr": "bonjour",
}
jobs := make(chan string)
go serve(messages, jobs)
go serve(messages, jobs)
jobs <- "en"
jobs <- "fr"
close(jobs)
}
The map is built before the goroutines start, and the go statement provides the happens-before edge, so each new goroutine sees the initialised map. Nothing writes to the map afterwards, and both goroutines only read it.
The dangerous bit is usually publication
Building a map in one goroutine and making it available to another is publication, and it needs synchronisation like any other shared state. A mutex unlock, channel send, goroutine creation, or suitable atomic operation can establish the required ordering.
This version has a race:
var current map[string]string
func reload() {
current = loadMessages()
}
func lookup(key string) string {
return current[key]
}
Returning a map from loadMessages is fine. The trouble is that one goroutine writes the shared variable current while another reads it, with nothing ordering the two. The map may be internally complete, but publishing the map header is still unsynchronised.
The race detector should make this visible:
go test -race ./...
A completed object can still be unsafely published. It's the same broad memory-model issue you meet with configuration structs, caches, and pointers: being immutable after construction doesn't excuse you from publishing the pointer or map variable safely.
Use a mutex when reloads are simple
A read-write mutex is often the clearest answer when configuration is read frequently and replaced occasionally. Hold the read lock while copying the map reference out, and the write lock while replacing it.
type Config struct {
mu sync.RWMutex
m map[string]string
}
func (c *Config) Lookup(key string) (string, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
value, ok := c.m[key]
return value, ok
}
func (c *Config) Replace(next map[string]string) {
c.mu.Lock()
defer c.mu.Unlock()
c.m = next
}
Once a string, integer, or other immutable value has been returned, there's no need to keep holding the read lock: the lookup copied it out while the map reference was protected. Don't apply that reasoning blindly to a returned pointer or slice, because the object behind it may still be shared.
It's also worth copying the incoming map before storing it, so the caller can't keep an alias and modify the supposedly read-only snapshot later:
func cloneMap(src map[string]string) map[string]string {
dst := make(map[string]string, len(src))
for key, value := range src {
dst[key] = value
}
return dst
}
Atomic snapshots work well for replace-only configuration
If the map is replaced as a whole and never edited in place, atomic.Value can make the ownership model explicit. Readers load one snapshot. Reloaders build a new map privately, then publish it in one operation.
type Messages struct {
value atomic.Value // stores map[string]string
}
func (m *Messages) Load(key string) (string, bool) {
messages := m.value.Load().(map[string]string)
value, ok := messages[key]
return value, ok
}
func (m *Messages) Store(next map[string]string) {
m.value.Store(cloneMap(next))
}
Initialise the value before any reader can call Load, and make sure every stored value has the same concrete type. The map passed to Store is cloned here because atomic.Value only protects publication; it can't control what the caller does with the original afterwards.
Quick detour: the same goes for an atomic pointer to a configuration struct. You get an atomic snapshot of the pointer and nothing more. The struct, its slices, its maps, and anything those fields point to are as mutable as they ever were. The snapshot boundary is only as strong as the ownership discipline behind it.
A read-only map can return mutable state
Consider a map of slices:
type Registry struct {
byName map[string][]string
}
func (r *Registry) Hosts(name string) []string {
return r.byName[name]
}
The map is never modified, but the returned slice shares its backing array with the registry. A caller can mutate that array while another goroutine is reading it, so the race has moved from the map operations to the slice users.
Return a copy when the registry owns the data:
func (r *Registry) Hosts(name string) []string {
hosts := r.byName[name]
return append([]string(nil), hosts...)
}
The same warning applies to pointer values, nested maps, byte slices, and interface values containing any of those. A map of strings is easy to make read-only; a map of rich objects needs an explicit ownership rule.
Do not confuse a copied map with a copied data structure
Assigning a map to another variable copies a small map descriptor. The entries themselves stay put:
copyOfMap := original
Both variables refer to the same underlying map. That also holds when a map sits inside a struct and the struct is copied. If one alias writes, every alias is part of the concurrent access problem.
Making a new map and copying the entries across gives you a new top-level map, but it's still only a shallow copy. If the values are slices or pointers, the nested objects remain shared.
What to check in a code review
- Find every writer. Search for assignments, deletes, increments, and mutations hidden inside helper functions.
- Check how the map reaches each goroutine. A channel, mutex, goroutine start, or atomic operation should make publication obvious.
- Inspect the value type. Slices, maps, pointers, byte buffers, and interfaces can expose mutable state.
- Check whether a reload replaces the map or edits it in place. Replacement supports snapshot designs; in-place edits require coordination with readers.
- Run
go test -race ./...under realistic tests. The race detector finds executions, so it cannot prove that an untested reload path is safe.
The official Go map guidance says concurrent map reads and writes must be synchronised. The Go memory model adds the part people tend to miss: shared data must also be published through a happens-before relationship.
So yes, remove the lock around a genuinely immutable map if profiling says it matters. Just make the immutability real, publish the snapshot safely, and don't hand out aliases that let somebody quietly turn your read-only map back into shared mutable state.