OhYee / blotter

Golang blog backend
34 stars 12 forks source link

about context #8

Open ics091 opened 1 year ago

ics091 commented 1 year ago

处理请求的时候好像没使用go原生的context,为啥?用这个HttpContext是因为能带更多信息?

  // HTTPContext context of a api call
  type HTTPContext struct {
      Request  *http.Request
      Response http.ResponseWriter
      buf      *bytes.Buffer
      header   []httpHeader
      user     *user.TypeDB
      userGot  bool
  }
OhYee commented 1 year ago

处理请求的时候好像没使用go原生的context,为啥?用这个HttpContext是因为能带更多信息?

  // HTTPContext context of a api call
  type HTTPContext struct {
    Request  *http.Request
    Response http.ResponseWriter
    buf      *bytes.Buffer
    header   []httpHeader
    user     *user.TypeDB
    userGot  bool
  }

可以用,不过本身context类型不太友好,需要再封装一层。 这里做成这样方便补全使用,如果需要用到 withCancel 等 context 特性也可以比较容易直接加进去