O'Reilly logo

Go Web Development Cookbook by Arpit Aggarwal

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it…

  1. Install the gopkg.in/mgo.v2, gopkg.in/mgo.v2/bson, and github.com/gorilla/mux packages, using the go get command, as follows:
$ go get gopkg.in/mgo.v2$ go get gopkg.in/mgo.v2/bson$ go get github.com/gorilla/mux
  1. Create delete-record-mongodb.go. Then we connect to MongoDB, get the name of an employee to be deleted from the database as an HTTP request variable, get the named collection, and remove the document, as follows:
package mainimport (  "fmt"  "log"  "net/http"  "github.com/gorilla/mux"  mgo "gopkg.in/mgo.v2"  "gopkg.in/mgo.v2/bson")const (  CONN_HOST = "localhost"  CONN_PORT = "8080"  MONGO_DB_URL = "127.0.0.1")var session *mgo.Sessionvar connectionError errortype Employee struct {  Id int `json:"uid"` Name string `json:"name"` ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required