package singleton type singleton struct { O interface{} } var instantiated *single = nil // private instance of singleton func New() *single { if instantiated == nil { instantiated = new(single) } return instantiated }
You can use an init() function to achieve most of this. For the rest, sync.Once will suite.
I wouldn't use that stack overflow pattern, its far to complicated, not type safe, and has a data race.
--
--
* You received this message because you are subscribed to the Google Groups "golang-nz" group.
* To post to this group, send email to gola...@googlegroups.com
* To subscribe from this group, send email to golang-nz...@googlegroups.com
* To unsubscribe from this group, send email to golang-nz+...@googlegroups.com
* For more options, visit this group at http://groups.google.com/group/golang-nz?hl=en-GB?hl=en-GB
---
You received this message because you are subscribed to the Google Groups "golang-nz" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nz+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
You received this message because you are subscribed to a topic in the Google Groups "golang-nz" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/golang-nz/98S8i41Ga7I/unsubscribe.
To unsubscribe from this group and all its topics, send an email to golang-nz+...@googlegroups.com.
There is a still a data race, on the value before the sync.Do.
Check the std lib for examples, sorry I can't be more forthcoming (phone)
Also, do you really need a singleton? Why not
var thing = Thing{...}
It would be rare to import a package with an expensive value constructed at init time and to not then use that value so lazy initialisation may be unnecessary.