路由组也可以嵌套,但是需要注意的是当进行嵌套时下一层的需要调用Group方法(设置路由前缀)是需要用上一层的进行调用:

 shopGroup := userGroup.Group("/shop")
代码
package main

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

// 路由的分组和嵌套
func main() {
	r := gin.Default()
	userGroup := r.Group("/user")
	{
		//分组
		userGroup.GET("/index", userIndex)
		userGroup.GET("/login", userLogin)

		// 嵌套
		shopGroup := userGroup.Group("/shop")
		{
			shopGroup.GET("/index", userShopIndex)
		}
	}
	r.Run()
}

func userIndex(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"message": "user/index",
	})
}

func userLogin(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"message": "user/login",
	})
}

func userShopIndex(c *gin.Context) {
	c.JSON(http.StatusOK, gin.H{
		"message": "/user/shop/index",
	})
}
控制台输出-启动服务: 网页访问

user/index:

user/login:

user/shop/index:

控制台输出-请求打印 参考链接