2

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

enter image description here

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 ?

Gagantous
  • 432
  • 6
  • 29
  • 69

2 Answers2

4

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")
    })
}
hao
  • 639
  • 5
  • 11
  • i got this error `routes\r_login.go:6:8: router.Get undefined (type *fiber.Router is pointer to interface, not interface)` – Gagantous Feb 09 '22 at 03:27
  • Just edit my answer, remove the * will do. – hao Feb 09 '22 at 03:30
  • but why i have to type fiber.Router while this repo https://github.com/vo9312/gofiber-starter/tree/48545ddee3b5d84d6d228e3d3dc27a3a468cec48, and see this its route said `*fiber.Group` https://github.com/vo9312/gofiber-starter/blob/48545ddee3b5d84d6d228e3d3dc27a3a468cec48/routes/users.go – Gagantous Feb 09 '22 at 03:44
  • 1
    Though the repo use fiber without v2, I believe it would not works, cause it pass an interface to an func which requires a pointer to a struct. – hao Feb 09 '22 at 03:56
  • thank sir, now i know and also it works – Gagantous Feb 09 '22 at 05:05
  • i cant find that information in fiber doc, how do u know that – Gagantous Feb 10 '22 at 06:05
  • 1
    read the code, and figure it out. – hao Feb 10 '22 at 07:25
1

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)
Flash Noob
  • 352
  • 3
  • 7