Skip to content

Instantly share code, notes, and snippets.

@knoguchi
Created August 19, 2019 15:35
Show Gist options
  • Save knoguchi/9de5d9905f6e6a414b536198f0eba8d6 to your computer and use it in GitHub Desktop.
Save knoguchi/9de5d9905f6e6a414b536198f0eba8d6 to your computer and use it in GitHub Desktop.
thread safe initialization of singleton. Better alternative of init()
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