Well the idea of a singleton is, that there is only one instance in the whole app (at least when you look at languages that support the keyword static). You do not need to set the singleton private - but then somebody has to take care of the initiation, which is totally find and usually done in DI frameworks or in main() when you provide your own dependencies.
The reason why I made the singleton private can be seen in the "thread-safe" example. The file takes control over thread handling in this example. Another way, if you want to let somebody else take control over how it is done, is that you need to make sure, that the singleton is initiated before anyone can access it.
I hope that answered your questions.
Hi and thanks to share this blog post. I have two questions: 1) In this example, you use private variable, why you don't expose it? in other word what difference between exposed and unexposed variable in this pattern? In my opinion when we expose it, other pkg can use it with nil value and as a result the application panics. is that the reason we use unexposed variable?
2) what happening if we don't expose type Singleton? In this example Singleton type is exposed, other pkg can create instance from Singleton type directly and as a result we have many instance from Singleton type.
thanks Matthias