Question

I have a webservice written in Go and currently I have this Global package I import everywhere which contains the connection to MongoDB (via MGO), but I have to say that this feels very icky to me. What is the best practice of maintaining connections to datasources in Go? I come from a PHP world, hence the Global :S

Was it helpful?

Solution

There is nothing wrong with importing a package which exports an initialized *mgo.Session object either as a package-level variable or through an accessor/initializer. Maybe the latter appeals better to classical "OOP-ists".

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top