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 github.com/gorilla/mux and github.com/gorilla/handlers packages using the go get command, as follows:
$ go get github.com/gorilla/mux$ go get github.com/gorilla/handlers
  1. Create http-rest-api.go, where we will define three routes—/status, /get-token and /employees—along with their handlers, as follows:
package mainimport (  "encoding/json"  "log"  "net/http"  "os"  "github.com/gorilla/handlers"  "github.com/gorilla/mux")const (  CONN_HOST = "localhost"  CONN_PORT = "8080")type Employee struct {  Id int `json:"id"`  FirstName string `json:"firstName"`  LastName string `json:"lastName"`}type Employees []Employeevar employees []Employeefunc init() {  employees = Employees  { Employee{Id: 1, FirstName: "Foo", LastName: "Bar"}, ...

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