Working with web handlers, requests, and ResponseWriters
Go defines HandlerFuncs
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.
Getting ready
Configure your environment according to these steps:
- Download and install Go on your operating system from https://golang.org/doc/install, and configure your
GOPATH
environment variable. - Open a terminal/console application.
- Navigate to your
GOPATH/src
and create a project...