Created
January 26, 2023 07:57
-
-
Save arriqaaq/47830944f43dd9c87847051b53d06fe9 to your computer and use it in GitHub Desktop.
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
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