【发布时间】:2020-05-30 21:06:01
【问题描述】:
我想传递 var 并在路由之间的每个控制器中全局更改它,所以如果控制器 1 将 var 更改为“as”,我希望控制器 2 使用 var =“as”的新值。我该怎么做?
像这样
main.go:
var test []string
func NewRouter() *gin.Engine {
gin.SetMode(gin.ReleaseMode)
r := gin.New()
r.GET("/ping", gets(test))
r.Run(":6030")
return r
}
controller.go:
func gets(test) gin.HandlerFunc {
fn := func(c *gin.Context){
// Here I want to change my var (test) and I want to change it globally so if any another function use it I want to use the new value I just change it here
}
}
【问题讨论】:
-
您需要使用指向要更改的值的指针,并且您需要使用互斥锁来避免数据竞争。