Standard library / sync/atomic

atomic.Int32.Swap()

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

Formal Syntax & Structure

go syntaxgo
func (x *Int32) Swap(new int32) (old int32)

Parameters & Attributes

NameTypeDescription
newint32Replacement value.

Return Value

(old int32)

Previous value.

Working Example

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