How to do it...

  1. Install the gopkg.in/mgo.v package, using the go get command, as follows:
$ go get gopkg.in/mgo.v
  1. Create connect-mongodb.go. Then we connect to the MongoDB database, get all the database names from the cluster, and write them to an HTTP response stream, as follows:
package mainimport (  "fmt"  "log"  "net/http"  "strings"  mgo "gopkg.in/mgo.v2")const (  CONN_HOST = "localhost"  CONN_PORT = "8080"  MONGO_DB_URL = "127.0.0.1")var session *mgo.Sessionvar connectionError errorfunc init() {  session, connectionError = mgo.Dial(MONGO_DB_URL)  if connectionError != nil   {    log.Fatal("error connecting to database :: ", connectionError)  }  session.SetMode(mgo.Monotonic, true)}func getDbNames(w http.ResponseWriter, r *http.Request) { db, err ...

Get Go Web Development Cookbook 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.