Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang "Best practice" to handle connection to database

Tags:

go

mgo

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

like image 788
Martijn van Maasakkers Avatar asked Apr 28 '14 06:04

Martijn van Maasakkers


People also ask

Is go good for database?

Go Database for Big Data and MicroservicesGo's built-in concurrency makes it a great choice for various large-scale applications, especially those involving Big Data processing and microservices architecture.

What database does Golang use?

As the C language of the 21st century, Go has good support for NoSQL databases, including the popular redis, mongoDB, Cassandra and Membase NoSQL databases.


1 Answers

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".

like image 172
thwd Avatar answered Sep 17 '22 12:09

thwd