Golang gin传递默认值

huangapple go评论83阅读模式
英文:

Golang gin pass a default value

问题

我有一个 API 和不同的路由,比如 /v1.1/test 和 /v1/test。对于这两个路由,我运行不同的工作版本,即 v1.1 或 v1。我的问题是如何将这个版本信息传递给路由器。

这是我的 main.go 文件:

v1 := router.Group("/v1")
{
    v1.GET("/test", getTest)
}

v1_1 := router.Group("/v1.1")
{
    v1_1.GET("/test", getTest)
}

在这里,我有一个名为 getTest 的函数:

func getTest(c *gin.Context) {
    fmt.Println(<我想要打印版本号>)
    task, err := zr.Push("test_v1", Test{Task: "exchanges"})
    getTestResponse(c, task, err)
}

我有一个可能的解决方案,可以使用闭包来解决,但我无法做到。

英文:

I have api and diffrenet routes like /v1.1/test and /v1/test for this two route I run different worker version which is v1.1 or v1, My question is how can I pass this version info to router

This is my main.go

   v1 := router.Group(&quot;/v1&quot;)
   {
   	v1.GET(&quot;/test&quot;, getTest)
)
   }

   v1_1 := router.Group(&quot;/v1.1&quot;)
   {

   	v1_1.GET(&quot;/test&quot;, getTest)
   }

In here I have getTest function

func getTest(c *gin.Context) {

    fmt.Println(&lt;I want to print version&gt;)
	task, err := zr.Push(&quot;test_v1&quot;, Test{Task: &quot;exchanges&quot;})
	getTestResponse(c, task, err)
}

And I have a possible solution which is using closure, may be can solve it, but I could not do it

答案1

得分: 2

gin可以通过Context在路径中处理参数,示例如下:

package main

import (
	"github.com/gin-gonic/gin"
	"net/http"
)

func main() {
	router := gin.Default()

	router.GET("/:version/test", getTest)

	router.Run(":8080")
}

func getTest(c *gin.Context) {
	version := c.Param("version")
	c.String(http.StatusOK, "version: %s\n", version)
}

输出结果如下:

$ curl 'http://localhost:8080/v1/test'  
version: v1
$ curl 'http://localhost:8080/v1.1/test'
version: v1.1

你可以在这里找到更多详细信息:https://github.com/gin-gonic/gin#querystring-parameters

英文:

gin can process parameters in path through Context like this:

package main

import (
	&quot;github.com/gin-gonic/gin&quot;
	&quot;net/http&quot;
)

func main() {
	router := gin.Default()

	router.GET(&quot;/:version/test&quot;, getTest)

	router.Run(&quot;:8080&quot;)
}

func getTest(c *gin.Context) {
	version := c.Param(&quot;version&quot;)
	c.String(http.StatusOK, &quot;version: %s\n&quot;, version)
}

output

$ curl &#39;http://localhost:8080/v1/test&#39;  
version: v1
$ curl &#39;http://localhost:8080/v1.1/test&#39;
version: v1.1

you can find more details here: https://github.com/gin-gonic/gin#querystring-parameters

答案2

得分: 1

警告:我不使用gin。但是请仍然参考以下内容。

闭包可能是解决方法。当你构建一个闭包时,要始终考虑需要哪种类型的函数,并创建一个返回该类型的函数。在你的情况下,你需要一个gin处理程序。

下面是一个根据版本不同而采取不同操作的示例:

func getTest(version string) func(c *gin.Context) {
    return func(c *gin.Context) {
        switch version {
        case "v1":
            // 处理旧版本的操作
        default:
            // 默认情况下执行其他操作
        }
    }
}

或者,如果你只是想像在你的简单示例中那样打印:

func getTest(version string) func(c *gin.Context) {
    return func(c *gin.Context) {
        fmt.Println(version)
        task, err := zr.Push("test_" + version, Test{Task: "exchanges"})
        getTestResponse(c, task, err)
    }
}

现在,你可以将其包装在你的路由器中:

v1 := router.Group("/v1")
{
    v1.GET("/test", getTest("v1"))
}

v1_1 := router.Group("/v1.1")
{
    v1_1.GET("/test", getTest("v1.1"))
}
英文:

Warning : I don't use gin. But see below nonetheless.

A closure may do the trick. When you build a closure, always try to think about what type of function you need, and create a function that will return this type. In your case, you need a gin handler.

Here is an example where you can act differently based on the version :

func getTest(version string) func(c *gin.Context) {
	return func(c *gin.Context) {
		switch version {
		case &quot;v1&quot;:
		// do what you need to do to handle old version
		default:
		// do something else by default
		}
	}
}

Or if you simply want to print like you do in your trivial example :

func getTest(version string) func(c *gin.Context) {
	return func(c *gin.Context) {
		fmt.Println(version)
		task, err := zr.Push(&quot;test_&quot; + version, Test{Task: &quot;exchanges&quot;})
		getTestResponse(c, task, err)
	}
}

Now, you can wrap that in your router :

v1 := router.Group(&quot;/v1&quot;)
{
	v1.GET(&quot;/test&quot;, getTest(&quot;v1&quot;))
}

v1_1 := router.Group(&quot;/v1.1&quot;)
{
	v1_1.GET(&quot;/test&quot;, getTest(&quot;v1.1&quot;))
}

huangapple
  • 本文由 发表于 2021年11月22日 21:59:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/70066957.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定