go-pattern-examples/creation/03_singleton/singleton.go

19 lines
282 B
Go
Raw Normal View History

2020-04-21 17:50:21 +03:00
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
}