Created
August 19, 2019 15:35
-
-
Save knoguchi/9de5d9905f6e6a414b536198f0eba8d6 to your computer and use it in GitHub Desktop.
thread safe initialization of singleton. Better alternative of init()
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
var sharedInstance *single | |
var once sync.Once | |
func GetInstance() *single { | |
once.Do(func() { | |
// init code here | |
sharedInstance = &single{} // instantiate | |
}) | |
return sharedInstance | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment