Go Sync Once
In this tutorial, you'll learn about Go Sync.Once: Not Executing. We cover key concepts, practical examples, and best practices.
Sync.Once usage -- Use sync.Once to ensure a function executes exactly once, even in concurrent scenarios.
The Problem
sync.Once.Do(f) guarantees f runs exactly once, even from multiple goroutines. It's safe for concurrent use and perfect for lazy initialization.
Wrong
var cfg *Config
func GetConfig() *Config {
if cfg == nil { // Race condition!
cfg = loadConfig()
}
return cfg
}
Output:
// Data race on cfg. Multiple loads of config.
Right
var (
once sync.Once
cfg *Config
)
func GetConfig() *Config {
once.Do(func() {
cfg = loadConfig()
})
return cfg
}
Output:
// Config loaded exactly once. Thread-safe.
Prevention
- Use sync.Once for one-time initialization
- The function passed to Do should not return error
- Use for singletons, lazy loading, one-time setup
- Reset the Once with sync.Once = sync.Once{} (rarely needed)
- sync.Once is safe for concurrent use
Common Mistakes with sync once
- Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
- Using
returnto exit a function early instead of wrapping a pure value in the monad - Mixing let bindings with <- bindings in do notation, producing type errors
These mistakes appear frequently in real-world GO code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of Doda Browser, DodaZIP, and Durga Antivirus Pro. DodaTech tutorials help Go developers build production-ready software used by millions.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro