gogo-echo

golang echo controller model


I am getting the following error when trying to factory my controller:

cannot use &(personController literal) (value of type *personController) as PersonController value in return statement: wrong type for method CreateNewPerson (have func(ctx github.com/labstack/echo/v4.Context) error, want func(ctx github.com/labstack/echo/v4.Context))

controller:

package controllers

import (
    "net/http"

    "github.com/labstack/echo/v4"
)

type personController struct{}

var (
// services
)

type PersonController interface {
    CreateNewPerson(ctx echo.Context)
    GetPerson(ctx echo.Context)
}

func NewPersonController() PersonController {
    return &personController{}
}

func (*personController) CreateNewPerson(ctx echo.Context) error {
    return ctx.JSON(http.StatusOK, "Hello")
}

func (*personController) GetPerson(ctx echo.Context) error {
    return ctx.JSON(http.StatusOK, "Hello")
}

and in my main func ai got this error :

func main() {
    e := echo.New()
    controller := controllers.NewPersonController()
    e.POST("/a", controller.CreateNewPerson)
}

cannot use controller.CreateNewPerson (value of type func(ctx echo.Contex


Solution

  • Your interface and struct implementation don't match. If you want your interface method's to match what's required by the echo framework then do this:

    type PersonController interface {
        CreateNewPerson(ctx echo.Context) error
        GetPerson(ctx echo.Context) error
    }