好好爱自己!

【转】Gin框架之路由分组和路由嵌套

原文:https://www.cnblogs.com/yh2924/p/12383892.html

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

 

----------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main
   
 
import (
    "github.com/gin-gonic/gin"
    "net/http"
)
 
func main() {
    r := gin.Default()
    userGroup := r.Group("/user")
    //{ //这两对大括号去掉也是可以的
        userGroup.GET("/index", func(c *gin.Context) {
            c.JSON(http.StatusOK, gin.H{
                "message": "user/index",
            })
        })
        userGroup.GET("/login", func(c *gin.Context) {
            c.JSON(http.StatusOK, gin.H{
                "message": "user/login",
            })
        })
        shopGroup := userGroup.Group("/shop")
        //{
            shopGroup.GET("/index", func(c *gin.Context) {
                c.JSON(http.StatusOK, gin.H{
                    "message": "/user/shop/index",
                })
            })
        //}
    //}
    r.Run()
}

  

 

 

 

 

posted @   立志做一个好的程序员  阅读(217)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2019-10-10 Multiple inheritance in Go
2019-10-10 Inheritance and subclassing in Go - or its near likeness
2019-10-10 golang Methods on structs
2018-10-10 【转】6 Reasons Why JavaScript’s Async/Await Blows Promises Away (Tutorial)
2017-10-10 【转】shell中的内建命令, 函数和外部命令
2017-10-10 clear out all variables without closing terminal
2017-10-10 linux中shell命令test用法和举例

不断学习创作,与自己快乐相处

点击右上角即可分享
微信分享提示