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

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

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.gor_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

我的文件夹结构如下:

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

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

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

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)

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:

确定