英文:
How to set Routes Group in Go Fiber V2 from separated file?
问题
我想为每个子主路由创建单独的文件。我正在使用Go 1.17版本。
main.go
package main
import (
"rolling_glory_go/routes"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
err := c.SendString("Hello golang!")
return err
})
routes.R_login(app.Group("/login"))
routes.R_users(app.Group("/users"))
app.Listen(":3000")
}
我想从r_login.go
和r_users.go
导入路由,这样我就可以从不同的文件中管理多个路由,而不是将许多路由放在一个文件中的main.go
中。我得到了以下错误。
.\main.go:17:26: cannot use app.Group("/login") (type fiber.Router) as type *fiber.Group in argument to routes.R_login: need type assertion
.\main.go:18:26: cannot use app.Group("/users") (type fiber.Router) as type *fiber.Group in argument to routes.R_users: need type assertion
我的文件夹结构如下:
r_login.go
package routes
import "github.com/gofiber/fiber/v2"
func R_login(router *fiber.Group) {
router.Get("/", func(c *fiber.Ctx) error {
return c.SendString("respond with a resource")
})
}
r_users.go
package routes
import "github.com/gofiber/fiber/v2"
func R_users(router *fiber.Group) {
router.Get("/", func(c *fiber.Ctx) error {
return c.SendString("respond with a resource")
})
}
如何修复这个问题?
英文:
I want to make seperate files for each sub main routes. I am using go 1.17
main.go
package main
import (
"rolling_glory_go/routes"
"github.com/gofiber/fiber/v2"
)
func main() {
app := fiber.New()
app.Get("/", func(c *fiber.Ctx) error {
err := c.SendString("Hello golang!")
return err
})
routes.R_login(app.Group("/login"))
routes.R_users(app.Group("/users"))
app.Listen(":3000")
}
I want to import routes from r_login.go
and r_users.go
so i could manage many routes from different files and not put many routes from single file in main.go
. I got an error like this.
.\main.go:17:26: cannot use app.Group("/login") (type fiber.Router) as type *fiber.Group in argument to routes.R_login: need type assertion
.\main.go:18:26: cannot use app.Group("/users") (type fiber.Router) as type *fiber.Group in argument to routes.R_users: need type assertion
My Structure Folder
r_login.go
package routes
import "github.com/gofiber/fiber/v2"
func R_login(router *fiber.Group) {
router.Get("/", func(c *fiber.Ctx) error {
return c.SendString("respond with a resource")
})
}
r_users.go
package routes
import "github.com/gofiber/fiber/v2"
func R_users(router *fiber.Group) {
router.Get("/", func(c *fiber.Ctx) error {
return c.SendString("respond with a resource")
})
}
How to fix this ?
答案1
得分: 4
由于您有app.Group("/login")
,其类型为fiber.Router
,只需修改R_login
函数以接受此类型。
package routes
import "github.com/gofiber/fiber/v2"
func R_login(router fiber.Router) {
router.Get("/", func(c *fiber.Ctx) error {
return c.SendString("respond with a resource")
})
}
英文:
As you have app.Group("/login")
which of type fiber.Router
, just modify R_login
to have it accept this type.
package routes
import "github.com/gofiber/fiber/v2"
func R_login(router fiber.Router) {
router.Get("/", func(c *fiber.Ctx) error {
return c.SendString("respond with a resource")
})
}
答案2
得分: 1
如果有人仍然遇到问题,我正在使用fiber/v2进行以下操作:
func BookRouter(router fiber.Router) {
router.Get("/", GetBook)
router.Get("/all", GetBooks)
router.Post("/", NewBook)
router.Delete("/", DeleteBook)
}
books := app.Group("/book") //server.go
books.Route("/", book.BookRouter)
请注意,这是一个代码片段,我已经将其翻译成中文。
英文:
if anyone is still having issues I'm doing this in fiber/v2
func BookRouter(router fiber.Router) {
router.Get("/", GetBook)
router.Get("/all", GetBooks)
router.Post("/", NewBook)
router.Delete("/", DeleteBook)
}
books := app.Group("/book") //server.go
books.Route("/", book.BookRouter)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论