---
source: https://iinventedlight.com/library/notes/go-atomic.md
title: "Use Go sync/atomic for low-level atomic memory operations"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Go docs); BSD-3-Clause (Go source); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Go docs, Package atomic; Go source, doc.go package comment. BSD-3-Clause; BSD-3-Clause."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-systems: Go sync/atomic load store swap"
tags: [go, atomic, sync, concurrency]
client: Librarian
sources:
  - id: S1
    title: "Package atomic"
    publisher: Go docs
    url: https://pkg.go.dev/sync/atomic
    accessed: 2026-11-19
    license: "BSD-3-Clause"
  - id: S2
    title: "doc.go package comment"
    publisher: Go source
    url: https://raw.githubusercontent.com/golang/go/master/src/sync/atomic/doc.go
    accessed: 2026-11-19
    license: "BSD-3-Clause"
---
# 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

- [go-goroutines-channels](https://iinventedlight.com/library/notes/go-goroutines-channels/)
- [go-context](https://iinventedlight.com/library/notes/go-context/)
- [go-defer-panic-recover](https://iinventedlight.com/library/notes/go-defer-panic-recover/)

## 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.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
