How to set Routes Group in Go Fiber V2 from separated file?

huangapple go评论128阅读模式
英文:

How to set Routes Group in Go Fiber V2 from separated file?

问题

我想为每个子主路由创建单独的文件。我正在使用Go 1.17版本。

main.go

  1. package main
  2. import (
  3. "rolling_glory_go/routes"
  4. "github.com/gofiber/fiber/v2"
  5. )
  6. func main() {
  7. app := fiber.New()
  8. app.Get("/", func(c *fiber.Ctx) error {
  9. err := c.SendString("Hello golang!")
  10. return err
  11. })
  12. routes.R_login(app.Group("/login"))
  13. routes.R_users(app.Group("/users"))
  14. app.Listen(":3000")
  15. }

我想从r_login.gor_users.go导入路由,这样我就可以从不同的文件中管理多个路由,而不是将许多路由放在一个文件中的main.go中。我得到了以下错误。

  1. .\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
  2. .\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

我的文件夹结构如下:

How to set Routes Group in Go Fiber V2 from separated file?

r_login.go

  1. package routes
  2. import "github.com/gofiber/fiber/v2"
  3. func R_login(router *fiber.Group) {
  4. router.Get("/", func(c *fiber.Ctx) error {
  5. return c.SendString("respond with a resource")
  6. })
  7. }

r_users.go

  1. package routes
  2. import "github.com/gofiber/fiber/v2"
  3. func R_users(router *fiber.Group) {
  4. router.Get("/", func(c *fiber.Ctx) error {
  5. return c.SendString("respond with a resource")
  6. })
  7. }

如何修复这个问题?

英文:

I want to make seperate files for each sub main routes. I am using go 1.17

main.go

  1. package main
  2. import (
  3. "rolling_glory_go/routes"
  4. "github.com/gofiber/fiber/v2"
  5. )
  6. func main() {
  7. app := fiber.New()
  8. app.Get("/", func(c *fiber.Ctx) error {
  9. err := c.SendString("Hello golang!")
  10. return err
  11. })
  12. routes.R_login(app.Group("/login"))
  13. routes.R_users(app.Group("/users"))
  14. app.Listen(":3000")
  15. }

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.

  1. .\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
  2. .\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

How to set Routes Group in Go Fiber V2 from separated file?

r_login.go

  1. package routes
  2. import "github.com/gofiber/fiber/v2"
  3. func R_login(router *fiber.Group) {
  4. router.Get("/", func(c *fiber.Ctx) error {
  5. return c.SendString("respond with a resource")
  6. })
  7. }

r_users.go

  1. package routes
  2. import "github.com/gofiber/fiber/v2"
  3. func R_users(router *fiber.Group) {
  4. router.Get("/", func(c *fiber.Ctx) error {
  5. return c.SendString("respond with a resource")
  6. })
  7. }

How to fix this ?

答案1

得分: 4

由于您有app.Group("/login"),其类型为fiber.Router,只需修改R_login函数以接受此类型。

  1. package routes
  2. import "github.com/gofiber/fiber/v2"
  3. func R_login(router fiber.Router) {
  4. router.Get("/", func(c *fiber.Ctx) error {
  5. return c.SendString("respond with a resource")
  6. })
  7. }
英文:

As you have app.Group("/login") which of type fiber.Router
, just modify R_login to have it accept this type.

  1. package routes
  2. import "github.com/gofiber/fiber/v2"
  3. func R_login(router fiber.Router) {
  4. router.Get("/", func(c *fiber.Ctx) error {
  5. return c.SendString("respond with a resource")
  6. })
  7. }

答案2

得分: 1

如果有人仍然遇到问题,我正在使用fiber/v2进行以下操作:

  1. func BookRouter(router fiber.Router) {
  2. router.Get("/", GetBook)
  3. router.Get("/all", GetBooks)
  4. router.Post("/", NewBook)
  5. router.Delete("/", DeleteBook)
  6. }
  7. books := app.Group("/book") //server.go
  8. books.Route("/", book.BookRouter)

请注意,这是一个代码片段,我已经将其翻译成中文。

英文:

if anyone is still having issues I'm doing this in fiber/v2

  1. func BookRouter(router fiber.Router) {
  2. router.Get("/", GetBook)
  3. router.Get("/all", GetBooks)
  4. router.Post("/", NewBook)
  5. router.Delete("/", DeleteBook)
  6. }
  7. books := app.Group("/book") //server.go
  8. books.Route("/", book.BookRouter)

huangapple
  • 本文由 发表于 2022年2月9日 09:42:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/71043030.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定