# Singleton Design Pattern in Go

DevFeed: [Singleton Design Pattern in Go](<https://devfeed.tech/articles/singleton-design-pattern-in-go-22060.md>)

Original publisher: [Read original article](<https://www.ardanlabs.com/blog/2013/07/singleton-design-pattern-in-go.html>)

Published: 2013-07-06T00:00:00Z

Content type: tutorial

Language: en

Sources: [William Kennedy](<https://devfeed.tech/sources/william-kennedy.md>)

Topics: [Go Language](<https://devfeed.tech/topics/go-language.md>), [Object-oriented programming (OOP)](<https://devfeed.tech/topics/oop.md>), [Polymorphism](<https://devfeed.tech/topics/polymorphism.md>), [Concurrency](<https://devfeed.tech/topics/concurrency.md>), [configuration](<https://devfeed.tech/topics/configuration.md>), [XML](<https://devfeed.tech/topics/xml.md>)

Tags: [ardan-labs](<https://devfeed.tech/tags/ardan-labs.md>), [blog](<https://devfeed.tech/tags/blog.md>), [configuration](<https://devfeed.tech/tags/configuration.md>), [design-patterns](<https://devfeed.tech/tags/design-patterns.md>), [go](<https://devfeed.tech/tags/go.md>), [go-programming](<https://devfeed.tech/tags/go-programming.md>), [golang](<https://devfeed.tech/tags/golang.md>), [oop](<https://devfeed.tech/tags/oop.md>), [polymorphism](<https://devfeed.tech/tags/polymorphism.md>), [programming](<https://devfeed.tech/tags/programming.md>), [thread](<https://devfeed.tech/tags/thread.md>), [xml](<https://devfeed.tech/tags/xml.md>)

## AI overview

This tutorial explains how to implement the Singleton design pattern in Go. It discusses managing shared resources in multithreaded applications, Go's package and type scoping, and the role of interfaces in providing polymorphic behavior without inheritance.

## Source excerpt

Multi-threaded applications are very complicated, especially when your code is not organized and consistent with how resources are accessed, managed and maintained. If you want to minimize bugs you need philosophies and rules to live by. Here are some of mine: Resource allocation and de-allocation should be abstracted and managed within the same type. Resource thread safeness should be abstracted and managed within the same type. A public interface should be the only means to accessing shared resources. Any thread that allocates a resource should de-allocated the same resource. In Go we don't have threads but Go Routines. The Go runtime abstracts the threading and task swapping of these routines. Regardless, the same philosophies and rules apply. One of my favorite design patterns is the Singleton. It provides a great implementation when you only need one instance of a type and that type manages shared resources. A Singleton is a design pattern where the type creates an instance of itself and keeps that reference private. Access to the shared resources managed by that reference is abstracted through a static pubic interface. These static methods also provide thread safeness. The application using the Singleton is responsible for initializing and de-initializing the Singleton but never has direct access to the internals. It escaped me for some time how to implement a Singleton in Go because Go is not a traditional object oriented programming language and there are no static methods. I consider Go to be a light object oriented programming language. Yes it does have encapsulation and type member functions but it lacks inheritance and therefore traditional polymorphism. In all of the OOP languages I have ever used, I never used inheritance unless I wanted to implement polymorphism. With the way interfaces are implemented in Go there is no need for inheritance. Go took the best parts of OOP, left out the rest and gave us a better way to write polymorphic code. In Go we c