Introducing mgo, a MongoDB driver for Go
mgo is a rich MongoDB driver that facilitates developers to write applications that talk to MongoDB without the need for the Mongo shell. The Go application can talk easily with MongoDB for all its CRUD operations using the mgo driver. It is an open-source implementation that can be used and modified freely. It is maintained by Labix. We can think it of as a wrapper around the MongoDB API. Installing the package is very simple, refer to the following command:
go get gopkg.in/mgo.v2This installs the package in $GOPATH. Now, we can refer the package to our Go programs, as follows:
import "gopkg.in/mgo.v2"
Let us write a simple program that talks to MongoDB and inserts The Dark Knight movie record:
package main
import (
"fmt"
"log"
mgo "gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
)
// Movie holds a movie data
type Movie struct {
Name string `bson:"name"`
Year string `bson:"year"`
Directors...