Golang Gin Router冲突conflicts with existing wildcard问题
作者:qq_38031013
Golang Gin路由冲突问题的解决策略,避免服务启动时的conflictswithexistingwildcard错误,提供多种实用方案
Golang Gin Router冲突
ctx.GET(“/v2.0/:user/go”) ctx.GET(“/v2.0/:package/go1”)
服务启动时会报错
conflicts with existing wildcard
以下可以解决
不适用于所有场景。
// 只定义一个API
pathGroup := ctx.Group("/v2.0/:path")
pathGroup.GET("/go", getPathHandler, goFunc)
pathGroup.GET("/go1", getPathHandler, goFunc1)
func getPathHandler(ctx *gin.Context) {
path := ctx.Param("path")
// 如果path的值和你期望的:user或者:package值一致
if path == "package1" {
ctx.Params = append(ctx.Params, gin.Param{Key: "package", Value: path})
} else {
ctx.Params = append(ctx.Params, gin.Param{Key: "user", Value: path})
}
ctx.Next()
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
