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是一个接口

  1. type Handler interface {
  2. ServeHTTP(ResponseWriter, *Request)
  3. }

The http.HandlerFunc 是一个类型:

  1. type HandlerFunc func(ResponseWriter, *Request)
  2. // ServeHTTP calls f(w, r).
  3. func (f HandlerFunc) ServeHTTP(w ResponseWriter, r *Request) {
  4. f(w, r)
  5. }

Gin

HandlerFunc
// HandlerFunc defines the handler used by gin middleware as return value.
type HandlerFunc func(*Context)

对HandlerFunc的进一步封装,入参为gin包下的Context