【設計模式】【golang源碼】 單例模式(Singleton Pattern)

單例模式目的:
對象初始化成本比較高,防止一個被屢次初始化。code

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
}
相關文章
相關標籤/搜索