// main.go
package main
import (
"gindemo02/router"
"github.com/gin-gonic/gin"
"net/http"
)
type ResContent struct {
Title string `json:"title"`
Content string `json:"content"`
Description string `json:"description"`
}
func main() {
r := gin.Default()
router.ApiRouterInit(r)
router.AdminRouterInit(r)
err := r.Run(":8001")
if err != nil {
return
}
}
// /project_name/router/adminRouter.go
package router
import (
"github.com/gin-gonic/gin"
"net/http"
)
func AdminRouterInit(r *gin.Engine) {
group := r.Group("/back")
{
group.POST("/login", func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{
"msg": "登录成功!",
})
})
// 嵌套分组
settingGroup := group.Group("/setting")
{
settingGroup.POST("/setting-one", func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{
"msg": "设置路由1",
})
})
}
}
}
// /project_name/router/apiRouter.go
package router
import (
"github.com/gin-gonic/gin"
"net/http"
)
func ApiRouterInit(context *gin.Engine) {
api := context.Group("/api")
{
api.POST("/login", func(context *gin.Context) {
context.JSON(http.StatusOK, gin.H{
"msg": "success",
})
})
}
}
标签:http,func,Group,抽离,嵌套,context,router,gin From: https://www.cnblogs.com/simdot/p/16586115.htmlsimdot 脉望