Go 单例模式


Lazy Loading:

type singleton struct {
}

// private
var instance *singleton

// public
func GetInstance() *singleton {
    if instance == nil {
        // not thread safe
        instance = &singleton{}
    }
    return instance
}

带锁:

type singleton struct {
}

var instance *singleton
var mu sync.Mutex

func GetInstance() *singleton {
    mu.Lock()
    defer mu.Unlock()
    if instance == nil {
        // unnecessary locking if instance already created
        instance = &singleton{}
    }
    return instance
}

带检查锁:

// <-- Not yet perfect. since it's not fully atomic
if instance == nil {
    mu.Lock()
    defer mu.Unlock()
    if instance == nil {
        instance = &singleton{}
    }
}
return instance

因为编译器优化没有检查实例存储状态。如果使用 sync/atomic 包的话,就可以自动帮我们加载和设置标记。

import "sync"
import "sync/atomic"

var initialized uint32

func GetInstance() *singleton {

    if atomic.LoadUInt32(&initialized) == 1 {
        return instance
    }

    mu.Lock()
    defer mu.Unlock()

    if initialized == 0 {
         instance = &singleton{}
         atomic.StoreUint32(&initialized, 1)
    }

    return instance
}

比较好的一种方式 sync.Once

import (
    "sync"
)
 
type singleton struct {
}
 
var instance *singleton
var once sync.Once
 
func GetInstance() *singleton {
    once.Do(func() {
        instance = &singleton{}
    })
    return instance
}
分享:

评论