mirror of
https://github.com/crazybber/awesome-patterns.git
synced 2024-11-26 23:06:03 +03:00
28 lines
368 B
Go
28 lines
368 B
Go
|
package internal
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"sync"
|
||
|
)
|
||
|
|
||
|
// singleton is private struct, it should be created and fetched by GetSingletonObject func
|
||
|
type singleton struct {
|
||
|
}
|
||
|
|
||
|
func (singleton) SayHi() {
|
||
|
fmt.Println("Hi!")
|
||
|
}
|
||
|
|
||
|
var (
|
||
|
once sync.Once
|
||
|
instance singleton
|
||
|
)
|
||
|
|
||
|
func GetSingletonObject() singleton {
|
||
|
once.Do(func() {
|
||
|
instance = singleton{}
|
||
|
})
|
||
|
|
||
|
return instance
|
||
|
}
|