go语言gin框架的路由分组

router.go

package main
import (
"github.com/gin-gonic/gin"
"fmt"
)

func main() {
r := gin.Default()
v1 := r.Group("/v1")
{
v1.GET("/login", login)
v1.GET("/submit", submit)
}
v2 := r.Group("/v2")
{
v2.POST("/login", login)
v2.POST("/submit", submit)
}
r.Run(":8001")
}

func login(c *gin.Context) {
  name := c.DefaultQuery("name", "jack")
  c.String(200, fmt.Sprintf("hello %s\n", name))
}

func submit(c *gin.Context) {
  name := c.DefaultQuery("name", "lily")
  c.String(200, fmt.Sprintf("hello %s\n", name))
}

运行结果:

[wj@localhost ~]$ curl 127.0.0.1:8001/v1/login
hello jack
[wj@localhost ~]$ curl 127.0.0.1:8001/v2/submit -X POST
hello lily


(完)