1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // OnceFunc returns a function that invokes f only once. The returned function
8 // may be called concurrently.
10 // If f panics, the returned function will panic with the same value on every call.
11 func OnceFunc(f func()) func() {
17 // Construct the inner closure just once to reduce costs on the fast path.
22 // Re-panic immediately so on the first call the user gets a
23 // complete stack trace into f.
28 valid = true // Set only if f does not panic
38 // OnceValue returns a function that invokes f only once and returns the value
39 // returned by f. The returned function may be called concurrently.
41 // If f panics, the returned function will panic with the same value on every call.
42 func OnceValue[T any](f func() T) func() T {
68 // OnceValues returns a function that invokes f only once and returns the values
69 // returned by f. The returned function may be called concurrently.
71 // If f panics, the returned function will panic with the same value on every call.
72 func OnceValues[T1, T2 any](f func() (T1, T2)) func() (T1, T2) {
90 return func() (T1, T2) {