Standard library / sync/atomic

atomic.Uint64.Swap()

Atomically replace the value and return the old value. Expected stdout: '12\n15\n'.

Formal Syntax & Structure

go syntaxgo
func (x *Uint64) Swap(new uint64) (old uint64)

Parameters & Attributes

NameTypeDescription
newuint64Replacement value.

Return Value

(old uint64)

Previous value.

Working Example

Example Snippetgo
package main
import (  "fmt"  "sync/atomic")
func main() { var x atomic.Uint64; x.Store(12); fmt.Println(x.Swap(15)); fmt.Println(x.Load()) }
Go Reference