uonce

package
v1.28.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 25, 2025 License: MIT Imports: 1 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Once

func Once[T any](constructor func() T) func() T

Once returns a function that always returns the same instance of T, initializing it only once using the provided constructor.

This utility is intended for implementing singletons in Go— it ensures the constructor is called exactly once, and all callers receive the same instance, even in the presence of concurrent calls.

Example (singleton pattern):

// Define a package-level singleton getter
getFoo := uonce.Once(func() *Foo {
	return &Foo{X: 123}
})

f1 := getFoo()
f2 := getFoo()
// f1 == f2 will be true

The returned function is safe for concurrent use from multiple goroutines.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL