Standard library / sync/atomic
atomic.SwapUintptr()
Atomically replace the value and return the old value. Raw pointer APIs require correctly aligned storage; prefer the corresponding typed atomic in new code. Expected stdout: '12\n15\n'.
Formal Syntax & Structure
go syntaxgo
func SwapUintptr(addr *uintptr, new uintptr) (old uintptr)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| addr | *uintptr | Pointer to the shared variable; satisfy architecture alignment requirements and use atomic access consistently. |
| new | uintptr | Replacement value. |
Return Value
(old uintptr)
Previous value.
Working Example
Example Snippetgo
package main
import ( "fmt" "sync/atomic")
func main() { x := new(uintptr) atomic.StoreUintptr(x, 12) fmt.Println(atomic.SwapUintptr(x, 15)) fmt.Println(atomic.LoadUintptr(x))}