我有这个实用程序:
type Handler struct{}
func (h Handler) Mount(router *mux.Router, v PeopleInjection) {
router.HandleFunc("/api/v1/people", h.makeGetMany(v)).Methods("GET")
}
上面这样称呼:
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) {
// ...
})
}
然后tc.ExtractType
是这样的:
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?
}
}
我的问题是 - serveHTTP 方法/函数来自哪里?
参数不h
只是具有此签名的函数:
func(w http.ResponseWriter, r *http.Request) { ... }
那么该函数是如何将ServeHTTP
函数附加到它的呢?
换句话说,我为什么打电话
h.ServeHTTP(w,r)
代替
h(w,r)
?