awesome-patterns/playground/singleton/internal/singleton.go
Bruce 01aad3573b
Practice singleton
Signed-off-by: Bruce <weichou1229@gmail.com>
2019-02-10 22:05:43 +08:00

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
}