7.7 The http.Handler
Interface
In Chapter 1, we saw a glimpse of how to use
the net/http
package to implement web clients
(§1.5) and servers (§1.7).
In this section, we’ll look more closely at the server API, whose
foundation is the http.Handler
interface:
package http type Handler interface { ServeHTTP(w ResponseWriter, r *Request) } func ListenAndServe(address string, h Handler) error
The ListenAndServe
function requires a server address, such
as "localhost:8000"
, and an instance
of the Handler
interface to which all requests should be
dispatched.
It runs forever, or until the server fails (or fails to start) with an
error, always non-nil, which it returns.
Imagine an e-commerce site with a database ...
Get The Go Programming Language now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.