Skip to content

Instantly share code, notes, and snippets.

@arriqaaq
Created January 26, 2023 07:57
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 arriqaaq/47830944f43dd9c87847051b53d06fe9 to your computer and use it in GitHub Desktop.
Save arriqaaq/47830944f43dd9c87847051b53d06fe9 to your computer and use it in GitHub Desktop.
type Singleton struct {
count int
}
var instance *Singleton
func GetInstance() *Singleton {
if instance == nil {
instance = &Singleton{}
}
return instance
}
func main() {
var wg sync.WaitGroup
wg.Add(10)
for i := 0; i < 10; i++ {
go func() {
defer wg.Done()
singleton := GetInstance()
singleton.count++
fmt.Println(singleton.count)
}()
}
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment