单例模式
Singleton Pattern
单例模式限制了一个类型只有一个对象。关于单例模式的饿汉模式和懒汉模式,线程安全的问题,在这里不做描述。
实现
单例定义
package singleton
type singleton map[string]string
var (
once sync.Once
instance singleton
)
func New() singleton {
once.Do(func() {
instance = make(singleton)
})
return instance
}
使用
func main(){
instance_1 := singleton.GetInstance()
instance_1["this"]="that"
instance_2 := singleton.GetInstance()
s := instance_2["this"]
fmt.Println(s)
}
Date:2018 03 10
许可协议:
CC BY 4.0