1

I'm trying to build an API in Go lang with fiber(V2) and gorm.

When I try to set up the routes with this code :

main.go (package main) :

func setupRoutes(app *fiber.App) {
    app.Get("/api/leads", lead.GetLeads)
    app.Get("/api/leads/:id", lead.GetLead)
    app.Post("/api/leads", lead.NewLead)
    app.Delete("/api/leads/:id", lead.DeleteLead)
}

lead.go (package lead) :

func GetLeads(c *fiber.Ctx) {
    db := database.BDConn
    var leads []Lead
    db.Find(&leads)
    c.JSON(leads)
}

I got go the error : cannot use lead.GetLeads (value of type func(c *fiber.Ctx)) as func(*fiber.Ctx) error value in argument to app.Get

Do you know how to fix that ?

  • 2
    `GetLeads` should return an `error` i.e. it should be declared as `func GetLeads(c *fiber.Ctx) error {` – MarcoLucidi Oct 23 '22 at 16:15
  • 1
    In addition to @MarcoLucidi comment, return `nil` after adding the return type of `error` as in the above comment. – Ahmad Oct 23 '22 at 19:33

0 Answers0