Golang - Singleton Pattern

Golang - Singleton Pattern

In software engineering, the Singleton pattern is a software design pattern that restricts the instantiation of a type to one object. This is useful when exactly one object is needed to coordinate actions across the system. In this article, we will discuss how to implement the Singleton pattern in Golang with an example code.

Singleton Pattern in Golang

The Singleton pattern is implemented in Golang using a struct type and a private package-level variable that holds the only instance of the struct. The struct's constructor function is exported and is used to return the instance of the struct. Here is an example implementation of the Singleton pattern in Golang:

package singleton

type Singleton struct {
    name string
}

var instance *Singleton

func GetInstance() *Singleton {
    if instance == nil {
        instance = &Singleton{name: "Golang Singleton"}
    }
    return instance
}

func (s *Singleton) GetName() string {
    return s.name
}

In the above implementation, we have defined a struct type Singleton with a single field name. We have also declared a private package-level variable named instance of type *Singleton. The GetInstance() function returns the instance of the Singleton struct. If the instance is nil, it creates a new instance of the Singleton struct and assigns it to the instance variable.

Finally, we have a method GetName() that returns the name of the Singleton struct.

Example Usage

Let's see how we can use the Singleton struct in our code.

In the above code, we have imported the Singleton package and created two instances s1 and s2 of the Singleton struct using the GetInstance() function. We then printed the name of both instances using the GetName() method. Finally, we checked if s1 and s2 are the same instances using the == operator.

package main

import "singleton"

func main() {
    s1 := singleton.GetInstance()
    s2 := singleton.GetInstance()

    println("s1.GetName()", s1.GetName())
    println("s2.GetName()", s2.GetName())

    println("s1 == s2", s1 == s2)
}

Thread-safe singleton

To create a thread-safe variant of the Singleton pattern in Golang, we can use the sync.Once package. We modify the instance variable to be of type sync.Once, and then use its Do() method to create a new instance of the Singleton struct only once. Here is an example implementation:

package singleton

import "sync"

type Singleton struct {
    name string
}

var (
    instance *Singleton
    once     sync.Once
)

func GetInstance() *Singleton {
    once.Do(func() {
        instance = &Singleton{name: "Safe Golang Singleton"}
    })
    return instance
}

func (s *Singleton) GetName() string {
    return s.name
}

In this implementation, we have declared a sync.Once variable named once. The GetInstance() function uses the once variable's Do() method to create a new instance of the Singleton struct only once. The GetName() method remains the same as before.

With this implementation, we ensure that only one instance of the Singleton struct is created, and that the creation is thread-safe.

You can use this implementation in the same way as before.

Conclusion

The Singleton pattern is a useful design pattern that restricts the instantiation of a type to one object. In this article, we have discussed how to implement the Singleton pattern in Golang with an example code. I hope this article helps you understand the Singleton pattern in Golang. Happy coding!

Did you find this article valuable?

Support Matthias Bruns by becoming a sponsor. Any amount is appreciated!