> For the complete documentation index, see [llms.txt](https://calvinfeng.gitbook.io/gonotebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://calvinfeng.gitbook.io/gonotebook/design-patterns/singleton.md).

# Singleton

This one is pretty common and straightforward.

> Singleton creational design pattern restricts the instantiation of a type to a single object.

Previously I built an army, now I wish to have a general that will lead my army. I just want one general because I don't want a mutiny.

```go
package army

var (
    once sync.Once
    commander *General
)

// Outside of this package, no one can access this struct except through NewGeneral()
type general struct {
    Army Army
}

func NewGeneral(armySize int) *general {
    once.Do(func() {
        commander = &general{Army: New(armySize)}
    })

    return commander
}
```
