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

jub0bs
  • 60,866
  • 25
  • 183
  • 186
Ming
  • 1,349
  • 4
  • 13
  • 36
  • Why have both a `personController` struct and `PersonController` interface? – Hymns For Disco Apr 24 '21 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? – Ming Apr 24 '21 at 20:27
  • As a side note, returning an interface is an anti-pattern: https://medium.com/@cep21/preemptive-interface-anti-pattern-in-go-54c18ac0668a – Tyler Kropp Apr 24 '21 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. – Ming Apr 24 '21 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](https://stackoverflow.com/a/67133656/11424673) – Hymns For Disco Apr 25 '21 at 10:54

1 Answers1

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
}
Christian
  • 1,676
  • 13
  • 19
  • 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 () – Ming Apr 24 '21 at 17:31
  • 1
    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 – Christian Apr 24 '21 at 17:34
  • they will have dependencies like commandbus and validator, but I'm a little confused on how to do this. – Ming Apr 24 '21 at 17:41
  • 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 https://www.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 – Christian Apr 24 '21 at 17:47