Implements a simplified version of Go's sync.Once for one-time initialization in concurrent programs.
Design a synchronization primitive that ensures a function is executed only once, even if called from multiple goroutines.
- Only one execution of the function, even with concurrent calls
- Safe for concurrent use
- No external libraries (standard Go only)
type Once struct {}
func (o *Once) Do(f func())var once Once
once.Do(func() {
println("This will only be printed once.")
})
once.Do(func() {
println("This will not be printed.")
})