其他分享
首页 > 其他分享> > Gin 路由分组(嵌套)

Gin 路由分组(嵌套)

作者:互联网

package main

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

func main() {
	router := gin.Default()

	v1 := router.Group("v1")
	{
		server1 := v1.Group("server1")
		{
			server1.GET("/user", func(ctx *gin.Context) {
				ctx.JSON(http.StatusOK, gin.H{"path": "/v1/server1/user"})
			})
			server1.GET("/name", func(ctx *gin.Context) {
				ctx.JSON(http.StatusOK, gin.H{"path": "/v1/server1/name"})
			})

		}
		server2 := v1.Group("server2")

		{

			server2.GET("/user", func(ctx *gin.Context) {
				ctx.JSON(http.StatusOK, gin.H{"path": "/v1/server2/user"})
			})
			server2.GET("/name", func(ctx *gin.Context) {
				ctx.JSON(http.StatusOK, gin.H{"path": "/v1/server2/name"})
			})
		}

	}

	v2 := router.Group("v2")
	{
		server1 := v2.Group("server1")
		{
			server1.GET("/user", func(ctx *gin.Context) {
				ctx.JSON(http.StatusOK, gin.H{"path": "/v2/server1/user"})
			})
			server1.GET("/name", func(ctx *gin.Context) {
				ctx.JSON(http.StatusOK, gin.H{"path": "/v2/server1/name"})
			})
		}

		server2 := v2.Group("server2")
		{
			server2.GET("/user", func(ctx *gin.Context) {
				ctx.JSON(http.StatusOK, gin.H{"path": "/v2/server2/user"})
			})
			server2.GET("/name", func(ctx *gin.Context) {
				ctx.JSON(http.StatusOK, gin.H{"path": "/v2/server2/name"})
			})
		}

	}

	err := router.Run(":8080")
	if err != nil {
		panic(err)
	}
}


标签:http,ctx,server1,server2,嵌套,func,gin,Gin,路由
来源: https://www.cnblogs.com/liy36/p/15202328.html