sync/atomic

Concurrency
import "sync/atomic"

Provides low-level atomic memory primitives useful for lock-free algorithms.

Example

package main

import (
    "fmt"
    "sync/atomic"
)

func main() {
    var counter int64
    atomic.AddInt64(&counter, 1)
    fmt.Println(atomic.LoadInt64(&counter))  // 1
}

Key Types & Functions

AddInt64LoadInt64StoreInt64CompareAndSwapInt64Value

About sync/atomic

The sync/atomic package (imported as sync/atomic) belongs to the Concurrency category of Go packages. Provides low-level atomic memory primitives useful for lock-free algorithms.

Go's standard library is one of the language's greatest strengths, providing production-ready implementations for networking, cryptography, encoding, I/O, and more. The sync/atomic package follows Go's philosophy of simplicity and composability — small, focused packages that combine through interfaces like io.Reader and io.Writer.

When using sync/atomic in production, follow Go best practices: handle errors explicitly, use context for cancellation and timeouts, prefer composition over inheritance, and write table-driven tests. The Go documentation at pkg.go.dev provides comprehensive API references and examples for every exported type and function.

Related Packages