Design Pattern: Singleton Pattern in Go

Singleton pattern is used to ensure that only an instance of a class is created. Each time an attempt to create an instance of the class will return the same instance.

Check details of Singleton Pattern at: Singleton Pattern Details.

Implementing the Singleton pattern in Go is slightly different from Java, PHP, and TypeScript. Let’s take a look at a few examples of Singleton design pattern in Go.

Example #1: Database Connection

Initialization

package main

import "fmt"

type connection struct {
	host     string
	port     string
	username string
	password string
}

type DbConnection interface {
	printInfo()
	executeQuery(query string)
}

var dbConnectionInstance *connection

Implement Interface

func (conn *connection) printInfo() {
	fmt.Println("host:", conn.host)
	fmt.Println("post:", conn.port)
	fmt.Println("username:", conn.username)
	fmt.Println("password:", conn.password)
}

func (conn *connection) executeQuery(query string) {
	fmt.Println("Executing: ", query)
}

Generate Single Instance

func GetDbInstance(host, port, username, password string) *connection {
	if dbConnectionInstance == nil {
		fmt.Println("Creating a new database instance")

		dbConnectionInstance = &connection{host, port, username, password}

		return dbConnectionInstance
	}

	fmt.Println("Returing existing database instance")
	return dbConnectionInstance
}

Demo

func main() {
	dbInstance1 := GetDbInstance("localhost", "3306", "root", "complex#password")
	dbInstance1.printInfo()

	dbInstance2 := GetDbInstance("localhost2", "2222", "root2", "complex#password2")
	dbInstance2.printInfo()

	if dbInstance1 == dbInstance2 {
		fmt.Println("dbInstance 1 and 2 are same instance")
	} else {
		fmt.Println("dbInstance 1 and 2 are different")
	}

}

Full Source Code

package main

import "fmt"

type connection struct {
	host     string
	port     string
	username string
	password string
}

type DbConnection interface {
	printInfo()
	executeQuery(query string)
}

var dbConnectionInstance *connection

func GetDbInstance(host, port, username, password string) *connection {
	if dbConnectionInstance == nil {
		fmt.Println("Creating a new database instance")

		dbConnectionInstance = &connection{host, port, username, password}

		return dbConnectionInstance
	}

	fmt.Println("Returing existing database instance")
	return dbConnectionInstance
}

func (conn *connection) printInfo() {
	fmt.Println("host:", conn.host)
	fmt.Println("post:", conn.port)
	fmt.Println("username:", conn.username)
	fmt.Println("password:", conn.password)
}

func (conn *connection) executeQuery(query string) {
	fmt.Println("Executing: ", query)
}

func main() {
	dbInstance1 := GetDbInstance("localhost", "3306", "root", "complex#password")
	dbInstance1.printInfo()

	dbInstance2 := GetDbInstance("localhost2", "2222", "root2", "complex#password2")
	dbInstance2.printInfo()

	if dbInstance1 == dbInstance2 {
		fmt.Println("dbInstance 1 and 2 are same instance")
	} else {
		fmt.Println("dbInstance 1 and 2 are different")
	}

}

Output

Creating a new database instance
host: localhost
post: 3306
username: root
password: complex#password

Returing existing database instance
host: localhost
post: 3306
username: root
password: complex#password

dbInstance 1 and 2 are same instance

Leave a Comment