我希望将我的routes.go分成多个文件,以便每个组都在自己的包中。有人能指出一些代码的例子,其中有人用Gin做过这个吗?
即。
package auth
...
auth = route.Group("/auth"){
auth.GET(...
auth.POST(...
}
...
package users
...
user = route.Group("/user"){
user.GET(...
user.POST(...
}
...
package main
import (
"auth"
"users"
)
...
router = gin.Default()
router.Register(auth.auth, users.user)
router.Run()
...
答案 0 :(得分:13)
执行此操作的方法是在每个中创建一个以路由作为参数的函数,然后将路由添加到参数中:
package auth
import "...gin"
func Routes(route *gin.Engine)
auth := route.Group("/auth"){
auth.GET(...
auth.POST(...
}
...
package users
import "...gin"
func Routes(route *gin.Engine)
user := route.Group("/user"){
user.GET(...
user.POST(...
}
...
package main
import (
"github.com/username/package/sub/auth"
"github.com/username/package/sub/users"
"github.com/gin-gonic/gin"
)
...
router := gin.Default()
auth.Routes(router) //Added all auth routes
user.Routes(router) //Added all user routes
router.Run()
...