# README
Understanding Go's sync
Package: Once
The sync
package in Go provides synchronisation primitives to ensure safe concurrent access to shared resources.
One of these primitives is the Once
type, which ensures that a piece of code is executed only once.
Table of Contents
Introduction to Once
The sync.Once
type is a type of synchronisation primitive used to ensure that a particular piece of code is executed
only once, regardless of how many goroutines attempt to execute it.
This is useful for initializing resources that are shared across multiple goroutines.
Usage of Once
Here's an example of how to use the sync.Once
type:
package main
import (
"fmt"
"sync"
)
var once sync.Once
func main() {
once.Do(initialize)
once.Do(initialize) // initialize will not be called again
}
func initialize() {
fmt.Println("Initializing...")
}
In this code:
- A
sync.Once
variable namedonce
is declared. - The
Do
method ofonce
is called with a functioninitialize
as an argument.
Theinitialize
function is executed the first timeDo
is called, but not the second time.
Examples and tests
See package
Use Cases
- Lazy Initialisation:
sync.Once
is useful for lazy initialisation where a resource is initialized only when it is needed.
- Singleton Pattern:
- Ensuring a single instance of a struct is created in a concurrent environment.
- One-time Setup:
- For setup that should only occur once but may be attempted from multiple goroutines.
Common Pitfalls
- Dependency Cycles:
- Be cautious of dependency cycles which could result in deadlocks when using
sync.Once
.
- Be cautious of dependency cycles which could result in deadlocks when using
- Error Handling:
sync.Once
does not provide built-in error handling, so if the initialisation function can fail, you'll need to handle errors manually.
Best Practices
- Error Handling:
- Establish a robust error handling mechanism when using
sync.Once
for critical initialisation.
- Establish a robust error handling mechanism when using
- Idempotency:
- Ensure the initialisation function is idempotent if it may be called multiple times outside of
a
sync.Once
context.
- Ensure the initialisation function is idempotent if it may be called multiple times outside of
a