go - 如何解决与现有 child 的问题冲突?

标签 go

我有路径:
GET /测试
GET / {test}
当我运行代码时,我得到

通配符段':test'与路径'/:test'中的现有子代冲突
怎么解决这个问题呢?
码:

r := gin.Default()
r.GET("/test", test1)
r.GET("/:test", test2)

最佳答案

方法1:

不同路径中的不同处理程序功能(即test1,test2)。

router := gin.Default()

router.GET("/test1", func(c *gin.Context) {
        // test1
    })

router.GET("/test2", func(c *gin.Context) {
        // test2
    })


方式2:

在路径中使用一个带有参数的处理函数。

router := gin.Default()

router.GET("/:test", func(c *gin.Context) {
        test := c.Param("test")
        if test == "test1" {
             // test1
        } else if test == "test2" {
             // test2
        }
    })

关于go - 如何解决与现有 child 的问题冲突?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60481870/

相关文章:

go - 如何在 Golang 中计算 256 位整数的 log16

postgresql - 无法使用 gorm Postgresql 设置外键

git - Cygwin 对环境变量使用相对和绝对路径

go - 在 Golang 中刷新标准输入缓冲区?

map - Go:什么决定了映射键的迭代顺序?

go - 在 Go1 之后有什么聪明的方法可以恢复 exp/html 吗?

go - 代码搜索中的正则表达式/语法在哪里?

concurrency - 没有看到 goroutines 的预期副作用

go - Go中函数名前的参数?

json - 无效操作 : type interface {} does not support indexing