Standard library / sync

sync.Map.LoadOrStore()

Return an existing value or insert a supplied value if absent. Expected stdout: '1 true\n'.

Formal Syntax & Structure

go syntaxgo
func (m *Map) LoadOrStore(key, value any) (actual any, loaded bool)

Parameters & Attributes

NameTypeDescription
keyanyComparable map key, or a package-specific comparable context key.
valueanyValue associated with the key.

Return Value

(actual any, loaded bool)

Actual value and whether it was already stored.

Working Example

Example Snippetgo
package main
import (  "fmt"  "sync")
func main() {  var m sync.Map  m.Store("a", 1)  v, loaded := m.LoadOrStore("a", 9)  fmt.Println(v, loaded)}
Go Reference