Standard library / sync/atomic
atomic.Value.Swap()
Replace the value and return its prior contents; the replacement must be non-nil and consistent in type. Expected stdout: 'first second\n'.
Formal Syntax & Structure
go syntaxgo
func (v *Value) Swap(new any) (old any)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| new | any | Replacement value. |
Return Value
(old any)
Previous value or nil.
Working Example
Example Snippetgo
package main
import ( "fmt" "sync/atomic")
func main() { var v atomic.Value; v.Store("first"); fmt.Println(v.Swap("second"), v.Load()) }