英文:
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定义中删除GetTodosParams并生成新文件,我可以减少到处理程序错误,但我不明白处理程序未定义的问题是什么:
type TodoServer struct{}
func (t TodoServer) GetTodos(w http.ResponseWriter, r *http.Request) {
// 从持久层检索所有待办事项的逻辑
w.WriteHeader(http.StatusOK)
}
func (t TodoServer) CreateTodo(w http.ResponseWriter, r *http.Request) {
// 将待办事项存储到持久层的逻辑
}
func (t TodoServer) DeleteTodo(w http.ResponseWriter, r *http.Request, todoId int32) {
// 从持久层删除待办事项的逻辑
}
func (t TodoServer) UpdateTodo(w http.ResponseWriter, r *http.Request, todoId int32) {
// 更新待办事项的逻辑
}
func main() {
s := TodoServer{}
h := Handler(s)
http.ListenAndServe(":3000", h)
}
我可以使用handlefunc()将handlestring链接到函数,但我想使用TodoServer,因为在Main Package中有类似的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启动它,但这并没有帮助我理解为什么接口函数不起作用。
英文:
I'm testing with OpenApi and as an example with https://github.com/d-vignesh/Todo-App-with-OpenAPI but if i run this, i get the following error:
.\main.go:9:77: undefined: GetTodosParams
.\main.go:31:19: undefined: Handler
If i delete the GetTodosParams in API-Definition and generate new files, I'm able to reduce to the handler error - but i don't understand the problem that the handler is undefined:
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)
}
I'm able to link per handlefunc() a handlestring to a function, but i want to use the TodoServer because there are the similiar interface function of Serverinterface in Main Package
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)
}
Instead i can start it with standard Servrhttp of net/http but this didn't help to understand why the interfacefunctions doesn't work
答案1
得分: 0
我猜你正在尝试以不推荐的非模块方式运行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
# 然后运行可执行文件:
Openapi-todo-app
英文:
I guess you are trying to run the file main.go
in a deprecated non-modules way. Try the recipe below.
Clone the repository:
git clone https://github.com/d-vignesh/Todo-App-with-OpenAPI
cd Todo-App-with-OpenAPI/
Run it like this:
go run github.com/d-vignesh/Openapi-todo-app
Build it like this:
go install github.com/d-vignesh/Openapi-todo-app
# Then run the executable:
Openapi-todo-app
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论