我正在使用 go openAPI 进行测试

I'm testing with go openAPI

我正在使用 OpenApi 进行测试,并以 https://github.com/d-vignesh/Todo-App-with-OpenAPI 为例,但是如果我 运行 这个,我会收到以下错误:

.\main.go:9:77: undefined: GetTodosParams
.\main.go:31:19: undefined: Handler

如果我删除 API-Definition 中的 GetTodosParams 并生成新文件,我可以减少处理程序错误 - 但我不明白处理程序未定义的问题:

type TodoServer struct{}

func (t TodoServer) GetTodos(w http.ResponseWriter, r *http.Request) {
    // our logic to retrieve all todos from a persistent layer
    w.WriteHeader(http.StatusOK)
}

func (t TodoServer) CreateTodo(w http.ResponseWriter, r *http.Request) {
    // our logic to store the todo into a persistent layer
}

func (t TodoServer) DeleteTodo(w http.ResponseWriter, r *http.Request, todoId int32) {
    // our logic to delete a todo from the persistent layer
}

func (t TodoServer) UpdateTodo(w http.ResponseWriter, r *http.Request, todoId int32) {
    // our logic to update the todo.
}

func main() {
    s := TodoServer{}
    h := Handler(s)

    http.ListenAndServe(":3000", h)
}

我能够link per handlefunc() 函数的句柄字符串,但我想使用 TodoServer,因为主包中有与 Serverinterface 类似的接口函数

type ServerInterface interface {

    // (GET /todos)
    GetTodos(w http.ResponseWriter, r *http.Request)

    // (POST /todos)
    CreateTodo(w http.ResponseWriter, r *http.Request)

    // (DELETE /todos/{todoId})
    DeleteTodo(w http.ResponseWriter, r *http.Request, todoId int32)

    // (PUT /todos/{todoId})
    UpdateTodo(w http.ResponseWriter, r *http.Request, todoId int32)
}

相反,我可以使用 net/http 的标准 Servrhttp 启动它,但这无助于理解为什么接口功能不起作用

我猜您正试图以一种已弃用的 non-modules 方式 运行 文件 main.go。试试下面的食谱。

克隆存储库:

git clone https://github.com/d-vignesh/Todo-App-with-OpenAPI
cd Todo-App-with-OpenAPI/

运行 像这样:

go run github.com/d-vignesh/Openapi-todo-app

像这样构建它:

go install github.com/d-vignesh/Openapi-todo-app

# Then run the executable:
Openapi-todo-app