go-pattern-examples/creation/03_singleton/singleton.go
2020-04-21 22:53:23 +08:00

19 lines
282 B
Go

package singleton
import "sync"
//Singleton 是单例模式类
type Singleton struct{}
var singleton *Singleton
var once sync.Once
//GetInstance 用于获取单例模式对象
func GetInstance() *Singleton {
once.Do(func() {
singleton = &Singleton{}
})
return singleton
}