Skip to content

Latest commit

 

History

History
37 lines (22 loc) · 397 Bytes

File metadata and controls

37 lines (22 loc) · 397 Bytes

[TOC]

使用场景

在进程的生命周期中,只被初始化一次

解决方案
  1. 定义原子操作变量

    var (
    	instance 	Config
    	once    	sync.Once
    )
    
    type Config struct {
    }
  2. 实现单例

    func NewConfig() *Config {
    	once.Do(func() {
    		instance = &Config{}
    	})
    
    	return instance
    }