Code:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
![](https://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif)
package main import ( "fmt" "sync" ) type Singleton struct{} var singleton *Singleton var once sync.Once func GetSingletonObj() *Singleton { once.Do(func() { fmt.Println("Create Obj") singleton = new(Singleton) }) return singleton } func main() { var wg sync.WaitGroup for i := 0; i < 5; i++ { wg.Add(1) go func() { obj := GetSingletonObj() fmt.Printf("%p\n", obj) wg.Done() }() } wg.Wait() }
Output:
Reproduced in: https: //www.cnblogs.com/believepd/p/11047229.html