Standard library / sync
sync.Once.Do()
Run the function on the first call only; other calls wait for it to finish. Recursive Do on the same Once deadlocks. Expected stdout: 'first\n'.
Formal Syntax & Structure
go syntaxgo
func (o *Once) Do(f func())Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| f | func() | Function to invoke; see callback timing and concurrency rules. |
Working Example
Example Snippetgo
package main
import ( "fmt" "sync")
func main() { var o sync.Once o.Do(func() { fmt.Println("first") }) o.Do(func() { fmt.Println("second") })}