LESSON · 1 OF 23

The HTTP handler signature

The HTTP handler signature

You have made HTTP requests as a client. Now you serve them. Go ships a full HTTP server in the standard library net/http, so you do not need a framework to stand up a working service.

The first piece is a handler. A handler is any function with this exact signature:

go
func hello(w http.ResponseWriter, r *http.Request) {
    // handle the request here
}

The two parameters are the whole job. r *http.Request holds everything about the incoming request - the path, the method, the query string, the headers, the body. w http.ResponseWriter is where you send the response back to the client.

Every handler you write, for every route, has this same two-parameter shape. Memorise it: func(w http.ResponseWriter, r *http.Request).

Spin up a fresh environment and practice live.
go · fresh machine · ready in under a minute