How it works...

Once we run the program, the HTTP server will start locally listening on port 8080.

Next, browsing to http://localhost:8080/employees will give you the list of all employees from the MongoDB employee collection:

Let’s look at the changes we introduced in the program:

  1. Using import ( "encoding/json" "log" "net/http" "github.com/gorilla/mux" mgo "gopkg.in/mgo.v2" "gopkg.in/mgo.v2/bson"), we imported an additional gopkg.in/mgo.v2/bson package, which is a BSON specification for Go, and the encoding/json package, which we used to marshal the object list, which we got from MongoDB, to JSON.
  2. Next, we defined a readDocuments handler, ...

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.