I have this utility:
type Handler struct{}
func (h Handler) Mount(router *mux.Router, v PeopleInjection) {
router.HandleFunc("/api/v1/people", h.makeGetMany(v)).Methods("GET")
}
the above calls this:
func (h Handler) makeGetMany(v PeopleInjection) http.HandlerFunc {
type RespBody struct {}
type ReqBody struct {
Handle string
}
return tc.ExtractType(
tc.TypeList{ReqBody{},RespBody{}},
func(w http.ResponseWriter, r *http.Request) {
// ...
})
}
and then tc.ExtractType
is like so:
func ExtractType(s TypeList, h http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
h.ServeHTTP(w, r) // <<< h is just a func right? so where does ServeHTTP come from?
}
}
my question is - where does the serveHTTP method/func come from??
isn't the h
parameter just a func with this signature:
func(w http.ResponseWriter, r *http.Request) { ... }
so then how does that func have the ServeHTTP
func attached to it?
In other words, why I am I calling
h.ServeHTTP(w,r)
instead of
h(w,r)
?
See Question&Answers more detail:os