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:
- 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.
- Next, we defined a readDocuments handler, ...