MCPcopy Create free account
hub / github.com/SKeyerror/Psyduck / GetConfig

Function GetConfig

go-concurrency/mutex/mutex_test.go:42–57  ·  view source on GitHub ↗

传统的双重校验锁方式,不过这种写法在其它一些语言中,例如 C++ 中,会因为指令重排的原因导致 Crash 所以其实现在很少有人会这么用,支持静态变量的语言都会利用“静态变量只会初始化一次”的特性来实现 而 Go 中则提供了 sync.Once 来实现单例

()

Source from the content-addressed store, hash-verified

40// 所以其实现在很少有人会这么用,支持静态变量的语言都会利用“静态变量只会初始化一次”的特性来实现
41// 而 Go 中则提供了 sync.Once 来实现单例
42func GetConfig() *Config {
43 var mutex sync.Mutex
44
45 if config == nil {
46 mutex.Lock()
47 if config == nil {
48 config = &Config{
49 Host: "127.0.0.1",
50 Port: 8080,
51 Workers: 4,
52 }
53 }
54 mutex.Unlock()
55 }
56 return config
57}
58
59func TestSingletonConfig(t *testing.T) {
60 var wg sync.WaitGroup

Callers 1

TestSingletonConfigFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected