Use Go sync/atomic for low-level atomic memory operations
Answer: Package atomic provides low-level atomic memory primitives. They are useful for implementing synchronization algorithms. These functions require great care to be used correctly. Synchronization is better done with channels or the facilities of the sync package.
Operations
The swap operation, implemented by the SwapT functions, is the atomic equivalent of storing a new value and returning the previous addr value. The compare-and-swap operation, implemented by the CompareAndSwapT functions, is the atomic equivalent of updating addr only if it equals old. The add operation, implemented by the AddT functions, adds delta to addr and returns addr. The load and store operations are implemented by the LoadT and StoreT functions. They are the atomic equivalents of return addr and of addr = val.
If the effect of an atomic operation A is observed also by atomic operation B, then A synchronizes before B. All the atomic operations executed in a program behave also as though executed in some sequentially consistent order. Only a few integer sizes are supported. Consider using the more ergonomic and less error-prone Int32.Swap instead.
| SwapT | AddT | |---|---| | SwapT | Store a new value and return the previous addr value | | CompareAndSwapT | compare-and-swap operation implemented by the CompareAndSwapT functions | | AddT | Add delta to addr and return addr | | LoadT | Atomic equivalent of return addr | | StoreT | Atomic equivalent of *addr = val |
See also
Claims
- id: C1 claim: Package atomic provides low-level atomic memory primitives. source: S1 evidence: "provides low-level atomic memory primitives"
- id: C2 claim: These atomic functions require great care to be used correctly. source: S2 evidence: "These functions require great care to be used correctly."
- id: C3 claim: CompareAndSwapT implements the compare-and-swap operation. source: S2 evidence: "The compare-and-swap operation, implemented by the CompareAndSwapT"
Sources
- [S1] Package atomic. Go docs. https://pkg.go.dev/sync/atomic . License: BSD-3-Clause. Accessed 2026-11-19.
- [S2] doc.go package comment. Go source. https://raw.githubusercontent.com/golang/go/master/src/sync/atomic/doc.go . License: BSD-3-Clause. Accessed 2026-11-19.
License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Go docs, Package atomic; Go source, doc.go package comment. BSD-3-Clause; BSD-3-Clause. Text was condensed and changed from the originals.
License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Go docs, Package atomic; Go source, doc.go package comment. BSD-3-Clause; BSD-3-Clause.
Cite this
iinventedlight. 2026. "Use Go sync/atomic for low-level atomic memory operations." iinventedlight Library, iil:note:arrmrsrj, version of 2026-11-19. https://iinventedlight.com/library/notes/go-atomic/. License: CC-BY-4.0.
@misc{iil_note_arrmrsrj,
author = {{iinventedlight}},
title = {{Use Go sync/atomic for low-level atomic memory operations}},
howpublished = {iinventedlight Library},
year = {2026},
date = {2026-11-19},
url = {https://iinventedlight.com/library/notes/go-atomic/},
note = {Permanent ID iil:note:arrmrsrj. License: CC-BY-4.0.},
}
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.
- go-atomic.table.json (table; CC-BY-4.0)
- go-atomic.schema.json (schema; CC-BY-4.0)
For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/go-atomic.md.