golang 回声控制器模型

golang echo controller model

我在尝试出厂控制器时遇到以下错误:

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))

控制器:

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")
}

并且在我的主要功能 ai 中出现了这个错误:

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

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

您的接口和结构实现不匹配。如果您希望您的接口方法与 echo 框架的要求相匹配,请执行以下操作:

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