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 it works…

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

Next, executing a POST request to create an employee document from the command line as follows will give you the ID of the document created in MongoDB:

$ curl -X POST http://localhost:8080/employee/create?name=foo\&id=1Last created document id is :: 1

Let’s look at the program we have written:

  1. Using import ( "fmt" "log" "net/http" "strconv" "github.com/gorilla/mux" mgo "gopkg.in/mgo.v2"), we imported github.com/gorilla/mux to create a Gorilla Mux Router and gopkg.in/mgo.v2 with the package alias name as mgo, which will act as a MongoDB driver.
  2. Next, we defined a createDocument handler, which fetches the name and ID of an employee from the HTTP ...

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