我正在使用 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)

}

我可以将每个 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 启动它,但这无助于理解为什么接口功能不起作用


慕婉清6462132
浏览 120回答 1
1回答

小唯快跑啊

main.go我猜您正在尝试以已弃用的非模块方式运行该文件。试试下面的食谱。克隆存储库:git clone https://github.com/d-vignesh/Todo-App-with-OpenAPIcd 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
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go