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!"))  })

Get Go Web Development Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.