mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-22 12:46:03 +03:00
Implement singleton pattern
This commit is contained in:
parent
a8f8670c1e
commit
b0918f796a
20
singleton/singleton.go
Normal file
20
singleton/singleton.go
Normal file
@ -0,0 +1,20 @@
|
||||
package singleton
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
type Object struct {
|
||||
}
|
||||
|
||||
var once sync.Once
|
||||
var instance *Object
|
||||
|
||||
func GetInstance() *Object {
|
||||
// Creates a singleton instance once.
|
||||
once.Do(func() {
|
||||
instance = &singleton{}
|
||||
})
|
||||
|
||||
return instance
|
||||
}
|
Loading…
Reference in New Issue
Block a user