mirror of
https://github.com/tmrts/go-patterns.git
synced 2026-11-11 23:07:51 +03:00
Implement singleton pattern
This commit is contained in:
1 parent
a8f8670c1e
commit
b0918f796a
1 file changed
+20
@@ -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
|
||||
}
|
||||
Reference in new issue
Block a user