路由群組(Group routes)是一種使用相同配置或參數註冊一組路由的方法,
本文參考範例之實作如下:
package main
import (
"net/http"
"github.com/gin-gonic/gin"
)
func main() {
// 初始化
router := gin.Default()
//建立router.Group
v1 := router.Group("/v1")
//建立router.Group
v2 := router.Group("/v2")
// Define a route for the root URL
v1.GET("/v11", Hello_World_v1) //localhost:8080/v1/v11
v1.GET("/v12", Hello_World_v2) //localhost:8080/v1/v12
v2.GET("/v12", Hello_World_v2) //localhost:8080/v2/v21
router.Run(":8080") // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}
func Hello_World_v1(c *gin.Context) {
c.String(http.StatusOK, "Hello, World V1!")
}
func Hello_World_v2(c *gin.Context) {
c.String(http.StatusOK, "Hello, World v2!")
}
1.https://github.com/gin-gonic/examples/tree/master/group-routes