How does one implement the Singleton design pattern in the go programming language?
I think that in a concurrent world we need to be a bit more aware that these lines are not executed atomically:
if instantiated == nil {
instantiated = new(single);
}
I would follow the suggestion of @marketer and use the package "sync"
import "sync"
type MySingleton struct {
}
var _init_ctx sync.Once
var _instance *MySingleton
func New() * MySingleton {
_init_ctx.Do( func () { _instance = new(MySingleton) } )
return _instance
}