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 package using the go get command, as follows:
$ go get github.com/gorilla/mux
  1. Create http-rest-put.go where we will define an additional route that supports the HTTP PUT method and a handler that either updates the employee details for the provided ID or adds an employee to the initial static array of employees; if the ID does not exist, marshal it to the JSON, and write it to an HTTP response stream, as follows:
package mainimport (  "encoding/json"  "log"  "net/http"  "github.com/gorilla/mux")const (  CONN_HOST = "localhost"  CONN_PORT = "8080")type Route struct {  Name string  Method string  Pattern string  HandlerFunc http.HandlerFunc}type Routes []Routevar routes = Routes{  Route  { "getEmployees", ...

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