如果使用header头中传版本参数具体步骤如下:
router.go内容如下:
func InitRouter() *gin.Engine {
router := gin.Default()
v101 := router.Group("/1.0.1")
{
v1_0_1.ArticleRegister(v101)
}
v102 := router.Group("/1.0.2")
{
v1_0_2.ArticleRegister(v102)
v1_0_2.UserRegister(v102)
}
return router
}
controller/v1.0.1/article.go
//路由注册
func ArticleRegister(router *gin.RouterGroup) {
article := ArticleController{}
router.GET("/index", article.Index)
router.GET("/list", article.List)
}
type ArticleController struct {
}
func (article *ArticleController) Index(c *gin.Context) {
c.JSON(0, "index")
return
}
func (article *ArticleController) List(c *gin.Context) {
c.JSON(0, "list101")
return
}
controller/v1.0.2/article.go
//路由注册
func ArticleRegister(router *gin.RouterGroup) {
article := ArticleController{}
oldarc := v1_0_1.ArticleController{}
router.GET("/index", oldarc.Index)
router.GET("/list", article.List)
}
type ArticleController struct {
}
func (article *ArticleController) Index(c *gin.Context) {
c.JSON(0, "index102")
return
}
func (article *ArticleController) List(c *gin.Context) {
c.JSON(0, "list102")
return
}
如果我在新版本中list方法改变了,就如果在要新版中兼容老版本的index。在ArticleRegister中,就要把之前的index给注册进来,我觉得我这种方法不怎么爽,如果接口少了还好办,如果接口多了就想要的增加了工作量。老师有什么好的兼容版本吗?