【设计模式】【golang源码】 单例模式(Singleton Pattern)

单例模式目的:
对象初始化成本比较高,防止一个被多次初始化。

var (
    confOnce sync.Once // guards init of confVal via initConfVal
    confVal  = &conf{goos: runtime.GOOS}
)

// systemConf returns the machine's network configuration.
func systemConf() *conf {
    confOnce.Do(initConfVal)
    return confVal
}

评论

暂无评论