How does one implement the Singleton design pattern in the go programming language?
Introduction: Singleton Design Pattern is a creational design pattern and also one of the most commonly used design pattern. This pattern is used when only a single instance of the struct should exist.
By using singletons in your project, you start to create technical debt. Singletons tend to spread like a virus because it's so easy to access them. It's difficult to keep track of where they're used and getting rid of a singleton can be a refactoring nightmare in large or complex projects.
A singleton should be used when managing access to a resource which is shared by the entire application, and it would be destructive to potentially have multiple instances of the same class. Making sure that access to shared resources thread safe is one very good example of where this kind of pattern can be vital.
Setting aside the argument of whether or not implementing the singleton pattern is a good idea, here's a possible implementation:
package singleton type single struct { O interface{}; } var instantiated *single = nil func New() *single { if instantiated == nil { instantiated = new(single); } return instantiated; }
single
and instantiated
are private, but New()
is public. Thus, you can't directly instantiate single
without going through New()
, and it tracks the number of instantiations with the private boolean instantiated
. Adjust the definition of single
to taste.
However, as several others have noted, this is not thread-safe, unless you're only initializing your singleton in init()
. A better approach would be to leverage sync.Once
to do the hard work for you:
package singleton import "sync" type single struct { O interface{}; } var instantiated *single var once sync.Once func New() *single { once.Do(func() { instantiated = &single{} }) return instantiated }
See also, hasan j's suggestion of just thinking of a package as a singleton. And finally, do consider what others are suggesting: that singletons are often an indicator of a problematic implementation.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With