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…

Let's implement logging using Gorilla handlers. Perform the following steps:

  1. Install the github.com/gorilla/handler and github.com/gorilla/mux packages using the go get command, as follows:
$ go get github.com/gorilla/handlers$ go get github.com/gorilla/mux
  1. Create http-server-request-logging.go and copy the following content:
package mainimport (  "net/http"  "os"  "github.com/gorilla/handlers"  "github.com/gorilla/mux")const (  CONN_HOST = "localhost"  CONN_PORT = "8080")var GetRequestHandler = http.HandlerFunc(  func(w http.ResponseWriter, r *http.Request)   {    w.Write([]byte("Hello World!"))  })var PostRequestHandler = http.HandlerFunc(  func(w http.ResponseWriter, r *http.Request)   {    w.Write([]byte("It's a Post Request!"))  })

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