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-error-handling.go, where we will create a custom handler that acts as a wrapper to handle all the HTTP requests, as follows:
package mainimport (  "errors"  "fmt"  "log"  "net/http"  "strings"  "github.com/gorilla/mux")const (  CONN_HOST = "localhost"  CONN_PORT = "8080")type NameNotFoundError struct {  Code int  Err error}func (nameNotFoundError NameNotFoundError) Error() string {  return nameNotFoundError.Err.Error()}type WrapperHandler func(http.ResponseWriter, *http.Request) errorfunc (wrapperHandler WrapperHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {  err := wrapperHandler(w, r) if err != ...

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