Last active
July 20, 2022 23:50
-
-
Save truongnmt/673c882cb2622df737ac9b7b077c5946 to your computer and use it in GitHub Desktop.
golang-singleton-new-4.go
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 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