Standard library / sync/atomic

atomic.StoreUint32()

Atomically replace the current value. Other shared data needs its own synchronization protocol. Raw pointer APIs require correctly aligned storage; prefer the corresponding typed atomic in new code. Expected stdout: '15\n'.

Formal Syntax & Structure

go syntaxgo
func StoreUint32(addr *uint32, val uint32)

Parameters & Attributes

NameTypeDescription
addr*uint32Pointer to the shared variable; satisfy architecture alignment requirements and use atomic access consistently.
valuint32Value to store; context values should be request-scoped.

Working Example

Example Snippetgo
package main
import (  "fmt"  "sync/atomic")
func main() {  x := new(uint32)  atomic.StoreUint32(x, 12)  atomic.StoreUint32(x, 15)  fmt.Println(atomic.LoadUint32(x))}
Go Reference