Created
August 20, 2020 20:04
-
-
Save nazariyv/672f0297c7cfd14237f9f5c3c3cc5923 to your computer and use it in GitHub Desktop.
go-singleton
This file contains hidden or 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 once sync.Once | |
| // type global | |
| type singleton map[string]string | |
| var instance singleton | |
| func NewClass() singleton { | |
| once.Do(func() { // <-- atomic, does not allow repeating | |
| instance = make(singleton) // <-- thread safe | |
| }) | |
| return instance | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment