Skip to content

Instantly share code, notes, and snippets.

@knoguchi
Created August 19, 2019 15:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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