MongoDB and the Go language

mgo is a popular MongoDB driver written in the Go language. The package page can be found at: http://labix.org/mgo. The driver is nothing more than a number of Go packages that facilitate writing Go programs capable of working with MongoDB.

In order to make use of mgo, the first step is to make use of the go get command to retrieve the package:

go get gopkg.in/mgo.v2

With the preceding command executed, we get the ability to use mgo in our code. We'd need to import the mgo package and the bson package that we discussed earlier. The package name we use to host our MongoDB persistence layer is called mongolayer.

Let's have a look at the mongolayer package:

package mongolayerimport (    mgo "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson" ...

Get Cloud Native programming with Golang now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.