我正在使用 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 启动它,但这无助于理解为什么接口功能不起作用
小唯快跑啊
相关分类