Standard library / sync

sync.Once

Run one initialization even with concurrent callers. Do not copy after use. A panic counts as completion for Do. Expected stdout: '1\n'.

Formal Syntax & Structure

go syntaxgo
type Once struct {}

Working Example

Example Snippetgo
package main
import (  "fmt"  "sync")
func main() { var o sync.Once; n := 0; o.Do(func() { n++ }); o.Do(func() { n++ }); fmt.Println(n) }
Go Reference