Standard library / sync

sync.Map.Range()

Visit keys until the callback returns false. Iteration order is unspecified and Range is not a consistent snapshot. Expected stdout: '3\n'.

Formal Syntax & Structure

go syntaxgo
func (m *Map) Range(f func(key, value any) bool)

Parameters & Attributes

NameTypeDescription
ffunc(key, value any) boolFunction to invoke; see callback timing and concurrency rules.

Working Example

Example Snippetgo
package main
import (  "fmt"  "sync")
func main() {  var m sync.Map  m.Store("a", 1)  m.Store("b", 2)  sum := 0  m.Range(func(k, v any) bool { sum += v.(int); return true })  fmt.Println(sum)}
Go Reference