【问题标题】:Is it possible to retrieve gin Context anywhere in the code (GoLang)?是否可以在代码(GoLang)的任何地方检索 gin 上下文?
【发布时间】:2022-12-20 08:07:07
【问题描述】:

我知道在 Java Springboot 中,可以在代码中的任何位置调用 SecurityContext 来获取上下文。

例如:

SecurityContext context = SecurityContextHolder.getContext();

我的问题是,是否可以在代码中的任何位置获取 *gin.Context 而不必将其作为参数传递给我的函数?

【问题讨论】:

    标签: go go-gin


    【解决方案1】:

    Golang 有另一个名为"context" 的包,它不依赖于"gin"。您可以在代码中的任何位置使用 context.Background()

    import (
        "context"
    )
    
    ...
    // sample usage for Mongodb 
    // operation which requires context
    
    resp, err := client.collection.InsertOne(
            context.Background(), // here
            ...,
    )
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-21
      • 1970-01-01
      • 2017-12-31
      • 2010-11-25
      • 2022-07-21
      • 2014-04-18
      • 1970-01-01
      • 2016-11-03
      相关资源
      最近更新 更多