Last active
December 19, 2022 03:45
-
-
Save maxclav/2200c92d3cf3ed362b90d1fae0bdd03f to your computer and use it in GitHub Desktop.
Singleton Design Pattern in Go (GoLang).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package designpattern | |
import ( | |
"sync" | |
) | |
type singleton struct {} | |
var instance *singleton | |
var once sync.Once | |
// We could also call this GetInstance(). | |
func Singleton() *singleton { | |
once.Do(func() { | |
instance = &singleton{} | |
}) | |
return instance | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment