← All notes

sync.Map: a map safe for concurrent goroutines

Answer: Map is safe for concurrent use by multiple goroutines. Loads, stores, and deletes run in amortized constant time. A Map must not be copied after first use. The zero Map is empty and ready for use.

When to use Map

Map is like a Go map of any to any, and multiple goroutines may use it concurrently without additional locking or coordination.

The type is specialized. For better type safety, most code should keep a plain Go map and add separate locking or coordination.

A better fit is a key written once and then read many times, as in a cache that only grows, or several goroutines that read, write, and overwrite disjoint sets of keys.

Those patterns can reduce lock contention versus pairing a Go map with a Mutex or RWMutex.

Methods

Load returns the value stored for a key, or nil if no value is present. The ok result indicates whether the value was found.

Store sets the value for a key.

Delete deletes the value for a key, and does nothing when the key is not in the map.

LoadOrStore returns the existing value for a present key. Otherwise it stores the given value and returns that.

Range calls a function sequentially for each key and value present.

Clear deletes all the entries, resulting in an empty Map.

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go, sync package reference; Go, sync/map.go source. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors. Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Go, sync package reference; Go, sync/map.go source. BSD-3-Clause (https://go.dev/LICENSE). Copyright The Go Authors.

Cite this

Permanent ID: iil:note:h3mpggn8

iinventedlight. 2026. "sync.Map: a map safe for concurrent goroutines." iinventedlight Library, iil:note:h3mpggn8, version of 2026-11-23. https://iinventedlight.com/library/notes/go-sync-map/. License: CC-BY-4.0.

@misc{iil_note_h3mpggn8,
  author = {{iinventedlight}},
  title = {{sync.Map: a map safe for concurrent goroutines}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-23},
  url = {https://iinventedlight.com/library/notes/go-sync-map/},
  note = {Permanent ID iil:note:h3mpggn8. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com. Or Report a correction (no account needed).

Extra files beside this note

These files use this note's license, shown below. Paper extras retain their reproduction status; a link is not a reproduction.

All artifact files (JSON index)

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-sync-map.md.