gin/routers-group/main.go

33 lines
567 B
Go
Raw Normal View History

2024-09-18 00:04:45 +08:00
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
//路由组处理GET请求
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(":8000")
}
func login(c *gin.Context) {
name := c.DefaultQuery("name", "Jack")
c.String(200, fmt.Sprintf("hello:%s", name))
}
func submit(c *gin.Context) {
name := c.DefaultQuery("name", "lily")
c.String(200, fmt.Sprintf("hello:%s", name))
}