Standard library / sync
sync.OnceValues()
Cache a pair of results, for example a value and an error. A returned error is cached too; a panic is repeated. Expected stdout: '7 <nil> 1\n'.
Formal Syntax & Structure
go syntaxgo
func OnceValues[T1, T2 any](f func() (T1, T2)) func() (T1, T2)Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| f | func() (T1, T2) | Function to invoke; see callback timing and concurrency rules. |
Return Value
func() (T1, T2)
Function returning the cached pair.
Working Example
Example Snippetgo
package main
import ( "fmt" "sync")
func main() { n := 0 f := sync.OnceValues(func() (int, error) { n++; return 7, nil }) v, e := f() f() fmt.Println(v, e, n)}