我正在学习 Go,目前正在尝试了解julienschmidt 的 httprouterhttp路由器的实际工作原理和工作原理。特别是 router.HandlerFunc() 的工作原理。
Go 的 http 包:
http.Handler:处理程序是一个接口。具有相同方法签名的任何类型,即 ServeHTTP(w,r) 实现一个处理程序。
http.Handle: http.Handle 是一个有两个参数的函数。1) 匹配请求路径的字符串, 2) Handler,调用它的 ServeHTTP() 方法。
http.HandlerFunc: 具有函数签名 (ResponseWriter, *Request) 和它自己的 ServeHTTP(w,r) 方法的自定义函数类型,满足 http.Handler 接口。
现在,如果我需要使用一个函数,例如。Bar(ResponseWriter, *Request) 满足作为 HandlerFunc,在 http.Handle 中,我键入 caste/covert 函数到 http.HandlerFunc,然后在 http.Handle 中使用它。像这样:
func Bar(w http.ResponseWriter, r *http.Request) {
    w.Write([]byte("My bar func response"))
}
...
mf := http.HandlerFunc(Bar)
http.Handle("/path", mf)查看http.HandleFunc 源代码,这就是 http.HandleFunc() 的工作原理。它需要一个函数作为 (w ResponseWriter, r *Request) 的签名并键入该函数。
像这样:
func (mux *ServeMux) HandleFunc(pattern string, handler func(ResponseWriter, *Request)) {    if handler == nil {        panic("http: nil handler")
    }
    mux.Handle(pattern, HandlerFunc(handler))
}julienschmidt 的 httprouterhttp
func (r *Router) HandlerFunc(method, path string, handler http.HandlerFunc) {
    r.Handler(method, path, handler)
}它不像 http.HandleFunc() 那样期望具有适当签名(ResponseWriter、*Request)的函数,而是期望一个 http.HandlerFunc。
同样没有类型转换/将函数转换为 HandlerFunc 类型,它只是将函数传递给另一个函数 router.Handler(method, path string, handler http.Handler),它再次需要一个 Handler。
所以我可以毫无问题地执行这段代码:
router.HandlerFunc(http.MethodPost, "/path", Bar) // Bar is not type casted/converted to a HandlerFunc
我可以理解将 Bar 类型转换为 HandlerFunc,然后将其传递给 router.HandlerFunc()。
您能否消除我的一些疑问:
如果不将 Bar() 类型转换为 HandlerFunc,如何满足作为 HandlerFunc 类型到 router.HandlerFunc() 的要求?
如果 router.Handler() 需要一个 http.Handler 类型,为什么没有类型转换为 http.HandlerFunc 的 Bar() 满足 router.Handler() 的处理程序?
我错过了什么?
 开满天机
开满天机 
					一只名叫tom的猫
 
					动漫人物
 随时随地看视频慕课网APP
随时随地看视频慕课网APP
相关分类