Context

context内部包含了*http.Request和http.ResponseWriter

  1. type Context struct {
  2. // 对内使用,可以用于接受http.ResponseWriter的值
  3. writermem responseWriter
  4. Request *http.Request
  5. // http.ResponseWriter的增强版
  6. Writer ResponseWriter
  7. Params Params
  8. handlers HandlersChain
  9. index int8
  10. fullPath string
  11. engine *Engine
  12. // This mutex protect Keys map
  13. mu sync.RWMutex
  14. // Keys is a key/value pair exclusively for the context of each request.
  15. Keys map[string]interface{}
  16. // Errors is a list of errors attached to all the handlers/middlewares who used this context.
  17. Errors errorMsgs
  18. //...
  19. }

路由

路由分组是基于Radix树,即基数树,也称压缩前缀树

gin 源码解析 - 图1

image.pngimage.png

  1. r.GET("/user/:id", test)
  2. r.GET("/user/:id/page", test) // ok
  3. r.GET("/user/name", test) // panic

点击查看【processon】