dqrl3595 2017-03-18 04:37
浏览 22
已采纳

如何像使用大猩猩上下文库一样使用Go中的新上下文包?

Go has come with a new package called context and with recent versions (Go 1.7 I think) we should be able to use it in the same way as gorilla/context package:

http://www.gorillatoolkit.org/pkg/context

With gorilla context you can very easily set and get variables that are relevant to a request, it's handlers, and middlewares.

To set a value in gorilla context is really easy:

func handleFunc(w http.ResponseWriter, r *http.Request) {
    context.Set(r, "foo", "bar")
}

To get the value we can do:

func handleFunc(w http.ResponseWriter, r *http.Request) {
    val := context.Get(r, "foo")
}

I understand that we can use this in middlewares so that the next middleware can use variables that were set in previous middleware. I would like to be able to do this with Go context package.

I understand that to get a value is quite simple like this:

func handleFunc(w http.ResponseWriter, r *http.Request) {
    fmt.Println(r.Context().Value("foo"))
}

But I have no idea how to set the value. It was not very intuitive for me and I don't really understand how to do it.

  • 写回答

1条回答 默认 最新

  • douye2111 2017-03-18 05:15
    关注

    See "Exploring the context package", using WithValue and the context associated to the Request:

    Middleware

    Middleware in Go refers to an http handler which wraps around a multiplexer. There are several 3rd party middleware solutions (such as negroni), but really the standard library supports a very similar pattern. The use of a Context in the request allows us to hold data in the request.

    See the example code for invocation and definition.

    func putClientIPIntoContext(r *http.Request) context.Context {
        ci := r.RemoteAddr
        fwd := r.Header.Get("X-Forwarded-For")
        if fwd != "" {
            ci = fwd
        }
        ctx := context.WithValue(r.Context(), ClientIPKey, ci)
        return ctx
    }
    

    The Context can store request-scoped variables.
    It’s useful when writing ‘middleware’, but it’s a little bit ‘anti-pattern’ — it’s a bit magical, because it’s not type-safe.

    See more at "Pitfalls of context values and how to avoid or mitigate them in Go".

    The example below only shows how you might use the authentication logic from above to verify that when a user is logged in when visiting any page with a path prefix of /dashboard/.
    A similar approach could be used to verify that a user is an admin before allowing them access to any page with a path prefix of /admin/.

    func requireUser(next http.Handler) http.Handler {  
      return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        user := lookupUser(r)
        if user == nil {
          // No user so redirect to login
          http.Redirect(w, r, "/login", http.StatusFound)
          return
        }
        ctx := context.WithValue(r.Context(), "user", user)
        next.ServeHTTP(w, r.WithContext(ctx))
      })
    }
    
    func main() {  
      dashboard := http.NewServeMux()
      dashboard.HandleFunc("/dashboard/hi", printHi)
      dashboard.HandleFunc("/dashboard/bye", printBye)
    
      mux := http.NewServeMux()
      // ALL routes that start with /dashboard/ require that a 
      // user is authenticated using the requireUser middleware
      mux.Handle("/dashboard/", requireUser(dashboard))
      mux.HandleFunc("/", home)
    
      http.ListenAndServe(":3000", addRequestID(mux))
    }
    

    As kostix comments, use Context wisely, like Dave Cheney suggest in "Context is for cancelation"

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格