1

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

5
  • Why have both a personController struct and PersonController interface? Commented Apr 24, 2021 at 18:56
  • @HymnsForDisco I need to pass my mediator as a dependency and then I thought of creating the mediator in the struct of the controller this is bad ? Can you help me? Commented Apr 24, 2021 at 20:27
  • As a side note, returning an interface is an anti-pattern: medium.com/@cep21/… Commented Apr 24, 2021 at 22:14
  • @TylerKropptt From what I understand in the article is it better to return structs and use interfaces in the parameters? i'm new to go, i would be happy if u could give me an example. Commented Apr 24, 2021 at 22:33
  • 1
    Yes, that is a good general rule (though there could be exceptions). You can also read my answer about a similar idea here Commented Apr 25, 2021 at 10:54

1 Answer 1

1

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
}
Sign up to request clarification or add additional context in comments.

4 Comments

thank u, do you think this approach is better to have to instantiate a person controller to be able to call or just use a var without the interface? like this personController.new () personController.get ()
I would probably just keep them as pure functions (e.g. not a method on a struct) if they don't have any dependencies as it keeps things simple
they will have dependencies like commandbus and validator, but I'm a little confused on how to do this.
If that is the case I would have a struct which has these dependencies in it, the methods can then "hang off" the struct. This article goes through different patterns for organising DB connections alexedwards.net/blog/organising-database-access but the same logic could be applied to other resources. I would just get something working and see what works best for your use case 🚀

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.