1 回答
TA贡献1921条经验 获得超9个赞
您需要将路由器变量存储在您的结构或全局变量中。然后各个 go 文件将向该变量添加处理程序。这是一个例子:
路线.go
package app
import (
"github.com/gin-gonic/gin"
)
type routes struct {
router *gin.Engine
}
func NewRoutes() routes {
r := routes{
router: gin.Default(),
}
v1 := r.router.Group("/v1")
r.addPing(v1)
r.addUsers(v1)
return r
}
func (r routes) Run(addr ...string) error {
return r.router.Run()
}
乒乓球
package app
import "github.com/gin-gonic/gin"
func (r routes) addPing(rg *gin.RouterGroup) {
ping := rg.Group("/ping")
ping.GET("/", pongFunction)
}
func pongFunction(c *gin.Context) {
c.JSON(200, gin.H{
"message": "pong",
})
}
用户.go
package app
import "github.com/gin-gonic/gin"
func (r routes) addUsers(rg *gin.RouterGroup) {
users := rg.Group("/users")
users.GET("/", getUsersFunction)
}
func getUsersFunction(c *gin.Context) {
c.JSON(200, gin.H{
"users": "...",
})
}
- 1 回答
- 0 关注
- 106 浏览
添加回答
举报