

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Still calibrating
click for more info
Not enough gems
Cost: 6 gems
1: Welcome to Learn HTTP Servers
incomplete
2: Goroutines in Servers
incomplete
3: Project Setup
incomplete
4: Server
incomplete
5: Fileservers
incomplete
6: Fileserver Quiz
incomplete
7: Serving Images
incomplete
8: Workflow Tips
incomplete
9: Custom Handlers
incomplete
10: Handler Review
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
An http.Handler is any defined type that implements the set of methods defined by the Handler interface, specifically the ServeHTTP method.
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
The ServeMux you used in the previous exercise is an http.Handler.
You will typically use a Handler for more complex use cases, such as when you want to implement a custom router, middleware, or other custom logic.
type HandlerFunc func(ResponseWriter, *Request)
You'll typically use a HandlerFunc when you want to implement a simple handler. The HandlerFunc type is just a function that matches the ServeHTTP signature above.
The Request argument is fairly obvious: it contains all the information about the incoming request, such as the HTTP method, path, headers, and body.
The ResponseWriter is less intuitive in my opinion. The response is an argument, not a return type. Instead of returning a value all at once from the handler function, we write the response to the ResponseWriter.