Skip to content

Instantly share code, notes, and snippets.

@truongnmt
Last active July 20, 2022 23:50
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 truongnmt/673c882cb2622df737ac9b7b077c5946 to your computer and use it in GitHub Desktop.
Save truongnmt/673c882cb2622df737ac9b7b077c5946 to your computer and use it in GitHub Desktop.
golang-singleton-new-4.go
var atomicinz uint64
// type global
type singleton map[string]string
var (
instance singleton
)
func NewClass() singleton {
for {
if atomic.LoadUint64(&atomicinz) == 1 {
return instance
}
if atomic.CompareAndSwapUint64(&atomicinz, 0, 1) {
instance = make(singleton)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment