Hi,
I have 3 years of experience writing go, but decades of writing C. I am reaching for best practices on using package private variables to implement signleton packages.
I work on a fairly large microservice project which uses protobufs over NATS for communication and ETCd for a key/val store etc. We have put together a repository of packages (a lbrary in C terms) of microservice support tools which mop up much common code in the microservices.
Doing this means our microservice code is a little cleaner but does hide some non-obvious "magic" in the library. The alternative is the microservices all need to create a set of global variables for the packages they reference and use those; which feels like unnecessary syntactic sugar.
I understand that this technique restricts the package to a single instance, however in this case this is fine; we never need more than one connection to NATS or ETCd.
I have read various blogs which argue one way or the other, do the Go authors (or any other esteemed authority) have any strong opinions to offer? Am I committing a crime against go, or am I using the power of the language to cleanly implement a feature?
-Steve