http
代码是如何定义的:
type Handler interface {
ServeHTTP(ResponseWriter, Request)
}
// 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.
type HandlerFunc func(ResponseWriter, Request)
Handeler的注释说了什么
HandlerFunc类型是一个适配器,允许将普通函数用作HTTP处理程序。若f是具有适当签名的函数(满足入参为ResponseWriter, *Request),则HandlerFunc(f)就是一个能够调用函数f的Handler。
function signature:指函数名称+参数列表,可以唯一确定一个函数(和别的函数不一样)
如何理解Handler和Handlergunc
http.Handler是一个接口
type Handler interface {
ServeHTTP(ResponseWriter, *Request)
}
The http.HandlerFunc 是一个类型:
type HandlerFunc func(ResponseWriter, *Request)
// ServeHTTP calls f(w, r).
func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
f(w, r)
}
Gin
HandlerFunc
// HandlerFunc defines the handler used by gin middleware as return value.
type HandlerFunc func(*Context)
对HandlerFunc的进一步封装,入参为gin包下的Context