Singleton Pattern in Go: Eager, Lazy, or Double-Checked?
Contact Author@:Medium,LinkedIn,Twitter
Hello, here is Wesley, Today’s article is about singleton pattern in Go. Without further ado, let’s get started.💪
Singleton Pattern, perhaps the most well-known design pattern among all. It’s also a type of creational pattern. When a struct only allows one instance to exist, we’ll use this design pattern. The unique instance of this struct is called the singleton object. It ensures that a class has only one instance and provides a global access point.
In Go language, the implementation of the singleton pattern is relatively simple, mainly achieved through the use of sync.Once
and a global variable to ensure thread safety.
What is Singleton Pattern
The core idea of the Singleton Pattern is:
- Uniqueness: There exists only one instance throughout the entire application.
- Global Access: Provides a global access point to obtain this instance.
Applicable Scenarios
- Need to control resource access, such as database connections, thread pools, etc.
- Application configuration management.
- Log manager, ensuring uniformity in log writing.
Go Language Implementation Methods
1. Eager Initialization (Based on init()
)
Create the singleton in the init()
function. Since each file’s init()
function in a package is only called once, this ensures that only one instance will be created.
However, creating the singleton instance at program startup time makes this method simple but may cause resource waste.
2. Lazy Initialization
Lazy initialization creates the instance on first use, using sync.Once
to ensure thread safety.
sync.Once
can be learned more about in this article: SyncOnce You Should Know in Golang - Tfrain - Tfrain’s Bilingual Blog
3. Double-Checked Locking (Double-Checked Locking)
Double-checked locking is an optimized implementation of lazy initialization, reducing the overhead of locking.
If you have read SyncOnce You Should Know in Golang - Tfrain - Tfrain’s Bilingual Blog, you will find that double-checked locking and using sync.Once
are not very different, as they both use similar structures and approaches, such as atomic variables and locks:
1 | done atomic.Uint32 |
sync.Once
is likely to be more performant than your own implementation of double-checked locking, and it’s also ready-to-use.
Summary
Advantages:
- Controls the creation of objects, avoiding resource waste.
- Provides a global access point for managing shared resources.
Disadvantages:
- Difficult to perform unit testing, as it depends on global state.
- Overuse of singletons can lead to code that is difficult to maintain and extend.
The implementation of the singleton pattern in Go language mainly relies on global variables and sync.Once
, ensuring thread safety and uniqueness. The choice of an appropriate implementation approach can be determined based on specific requirements:
- Eager initialization is suitable for situations where instances need to be created at startup time.
- Lazy initialization and double-checked locking are more suitable for scenarios that require delayed initialization and concurrent safety.
More
Recent Articles:
- Factory Pattern You Should Know in Golang(Design Patterns 02) on Medium on Website
- Builder Pattern You Should Know in Golang(Design Patterns 01) on Medium on Website
Random Article:
More Series Articles about You Should Know In Golang:
https://wesley-wei.medium.com/list/you-should-know-in-golang-e9491363cd9a
And I’m Wesley, delighted to share knowledge from the world of programming.
Don’t forget to follow me for more informative content, or feel free to share this with others who may also find it beneficial. it would be a great help to me.
Give me some free applauds, highlights, or replies, and I’ll pay attention to those reactions, which will determine whether or not I continue to post this type of article.
See you in the next article. 👋
中文文章: https://programmerscareer.com/zh-cn/golang-singleton-pattern/
Author: Medium,LinkedIn,Twitter
Note: Originally written at https://programmerscareer.com/golang-singleton-pattern/ at 2024-11-23 22:23.
Copyright: BY-NC-ND 3.0
Comments