Skip to content

Commit

Permalink
update singleton
Browse files Browse the repository at this point in the history
1. 修改mutex为sync.Once
2. 命名更加golang语言化
  • Loading branch information
xpzouying authored and senghoo committed Feb 24, 2018
1 parent cf32d44 commit b55d7cf
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions 03_singleton/singleton.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@ import "sync"
//Singleton 是单例模式类
type Singleton struct{}

var singletonInst *Singleton
var singletonInstLock sync.Mutex
var singleton *Singleton
var once sync.Once

//GetInstance 用于获取单例模式对象
func GetInstance() *Singleton {
if singletonInst == nil {
singletonInstLock.Lock()
if singletonInst == nil {
singletonInst = &Singleton{}
}
}
return singletonInst
once.Do(func() {
singleton = &Singleton{}
})

return singleton
}

0 comments on commit b55d7cf

Please sign in to comment.