goland单例模式

使用懒惰模式的单例模式,使用双重检查加锁保证线程安全

singleton.go

package singleton

import "sync"

//Singleton 是单例模式类
type Singleton struct{
     }

var singleton *Singleton
var once sync.Once

//GetInstance 用于获取单例模式对象
func GetInstance() *Singleton {
     
    once.Do(func() {
     
        singleton = &Singleton{
     }
    })

    return singleton
}

singleton_test.go

package singleton

import (
    "sync"
    "testing"
)

const parCount = 100

func TestSingleton(t *testing.T) {
     
    ins1 := GetInstance()
    ins2 := GetInstance()
    if ins1 != ins2 {
     
        t.Fatal("instance is not equal")
    }
}

func TestParallelSingleton(t *testing.T) {
     
    wg := sync.WaitGroup{
     }
    wg.Add(parCount)
    instances := [parCount]*Singleton{
     }
    for i := 0; i < parCount; i++ {
     
        go func(index int) {
     
            instances[index] = GetInstance()
            wg.Done()
        }(i)
    }
    wg.Wait()
    for i := 1; i < parCount; i++ {
     
        if instances[i] != instances[i-1] {
     
            t.Fatal("instance is not equal")
        }
    }
}
```

你可能感兴趣的:(goland单例模式)