There are a couple of concepts related to services.
The core interface for handline HTTP request.
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
You can turn a function (with the right signature) into a type, that satisfies http.Handler.
The HandlerFunc type is an adapter to allow the use of ordinary functions as HTTP handlers. If f is a function with the appropriate signature, HandlerFunc(f) is a Handler that calls f.
With first class functions, it is possible to start an HTTP handler with a single (long) line.
If we type http.HandleFunc
, we are working on the DefaultServeMux
, a
http.ServeMux added for convenience
in the http package.
ServeMux is an HTTP request multiplexer. It matches the URL of each incoming request against a list of registered patterns and calls the handler for the pattern that most closely matches the URL.
The ServeMux
is itself an HTTP handler, as it implements
ServeHTTP.
- Example 1
- Example 2
- Example 3
- Example 4
- Example 4
- Example 5
- Example 6
- Example 7
- Example 8
- Example 9
- Example 10
- Example 11
A todo web application.