我正在使用go openAPI进行测试。

huangapple go评论78阅读模式
英文:

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

huangapple
  • 本文由 发表于 2022年5月14日 15:17:51
  • 转载请务必保留本文链接:https://go.coder-hub.com/72238129.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定