Golang's "Best Practice" for Handling a Database Connection

I have a web service written in Go, and currently I have this global package that I import everywhere that contains a connection to MongoDB (via MGO), but I have to say that it is very unpleasant for me. What is the best practice for maintaining data source connections in Go? I come from the world of PHP, so Global: S

+4
source share
1 answer

There is nothing wrong with importing a package that exports an initialized object *mgo.Sessioneither as a package level variable or through an accessor / initializer. Maybe the latter appeals better to the classic OOP-Itzam.

+7

Source: https://habr.com/ru/post/1538346/


All Articles