Go 设计模式--单例模式
Go单例模式
package main
import(
"fmt"
"sync"
"time"
)
type Singleton struct {}
var singleton *Singleton
//只会创建一次
var once sync.Once
func GetInstance() *Singleton{
once.Do(func(){
singleton = &Singleton{}
})
fmt.Printf("GetInstance =%p\n",singleton)
return singleton
}
func main(){
go GetInstance()
go GetInstance()
go GetInstance()
go GetInstance()
time.Sleep(time.Second*2)
}

浙公网安备 33010602011771号