Home >Backend Development >Golang >Singleton pattern with Go generics
I'm trying to figure out the least worst way to implement a singleton for a generic variable in golang. Using the plain sync.once
pattern with global variables won't work because the common type information isn't available there (below).
This example is contrived, but in practice, the code that maintains the singleton can be separated from the client code that defines t
(e.g. in a library).
Assume this library code, where the specific value of t is unknown:
type cache[t any] struct{} var ( cacheonce sync.once cache cache[any] // global singleton ) func getorcreatecache[t any]() cache[t] { cacheonce.do(func() { typedcache := buildcache() cache = typedcache.(cache[any]) // invalid type assertion }) return cache.(cache[t]) // invalid type assertion }
And assume this separate client code where t
is defined as string
:
stringCache := getOrCreateCache[string]()
What is the best way to achieve this?
I ended up solving this problem using the atomic.pointer
api and integrated it into a simple library for Anyone else interested use: One-line. Reprocessing the original post using singlets looks like this:
Sample library code:
type cache[t any] struct{} var singleton = &singlet.singleton{} func getorcreatecache[t any]() (cache[t], err) { return singlet.getordo(singleton, func() cache[t] { return cache[t]{} }) }
Client code:
stringCache := getOrCreateCache[string]()
And the singlet library code that supports this feature:
var ErrTypeMismatch = errors.New("the requested type does not match the singleton type") type Singleton struct { p atomic.Pointer[any] mtx sync.Mutex } func GetOrDo[T any](singleton *Singleton, fn func() T) (result T, err error) { maybeResult := singleton.p.Load() if maybeResult == nil { // Lock to guard against applying fn twice singleton.mtx.Lock() defer singleton.mtx.Unlock() maybeResult = singleton.p.Load() // Double check if maybeResult == nil { result = fn() var resultAny any = result singleton.p.Store(&resultAny) return result, nil } } var ok bool result, ok = (*maybeResult).(T) if !ok { return *new(T), ErrTypeMismatch } return result, nil }
I hope this helps others who encounter this situation.
The above is the detailed content of Singleton pattern with Go generics. For more information, please follow other related articles on the PHP Chinese website!