我想為每個(gè)子主要路線(xiàn)制作單獨(dú)的文件。我正在使用 go 1.17main.gopackage mainimport ( "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")}我想從中導(dǎo)入路由r_login.go,r_users.go因此我可以管理來(lái)自不同文件的許多路由,而不是將單個(gè)文件中的許多路由放入main.go. 我收到這樣的錯(cuò)誤。.\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我的結(jié)構(gòu)文件夾r_login.gopackage routesimport "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.gopackage routesimport "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") })}如何解決這個(gè)問(wèn)題?
如何從單獨(dú)的文件中設(shè)置 Go Fiber V2 中的路由組?
函數(shù)式編程
2022-11-08 14:28:36