Go defines HandlerFunc and a Handler interface with the following signatures:
// HandlerFunc implements the Handler interface
type HandlerFunc func(http.ResponseWriter, *http.Request)
type Handler interface {
ServeHTTP(http.ResponseWriter, *http.Request)
}
By default, the net/http package makes extensive use of these types. For example, a route can be attached to a Handler or HandlerFunc interface. This recipe will explore creating a Handler interface, listening on a local port, and performing some operations on an http.ResponseWriter interface after processing http.Request. This should be considered the basis for Go web applications and RESTful APIs.
How to do it...
The following steps cover the writing and running of your application:
- From your Terminal or console application,createa new directory called~/projects/go-programming-cookbook/chapter8/handlers, and...