Standard library / sync/atomic

atomic.Uintptr.Swap()

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

Formal Syntax & Structure

go syntaxgo
func (x *Uintptr) Swap(new uintptr) (old uintptr)

Parameters & Attributes

NameTypeDescription
newuintptrReplacement value.

Return Value

(old uintptr)

Previous value.

Working Example

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