0

I am a newbie in go and go-swagger. I am following steps in Simple Server tutorial in goswagger.io.

I am using Ubuntu 18.04, swagger v0.25.0 and go 1.15.6.

  1. Following the same steps, there are a few differences of the files generated. For instance, goswagger.io's has find_todos_okbody.go and get_okbody.go in models but mine does not. Why is that so?

Link to screenshot of my generated files vs Link to screenshot of generated files by swagger.io

  1. Starting the server as written in the tutorial go install ./cmd/todo-list-server/ gives me the following error. Can anyone please help with this?
# my_folder/swagger-todo-list/restapi
restapi/configure_todo_list.go:41:8: api.TodosGetHandler undefined (type *operations.TodoListAPI has no field or method TodosGetHandler)
restapi/configure_todo_list.go:42:6: api.TodosGetHandler undefined (type *operations.TodoListAPI has no field or method TodosGetHandler)
  1. The first step in goswagger.io todo-list is swagger init spec .... Which directory should I run this command in? I ran it in a newly created folder in my home directory. However, from the page, it shows the path to be ~/go/src/github.com/go-swagger/go-swagger/examples/tutorials/todo-list. I am not sure whether I should use go get ..., git clone ... or create those folders. Can someone advise me?

Thanks.

AEWRocks
  • 157
  • 1
  • 2
  • 10

3 Answers3

2
  1. This is likely the documentation lagging behind the version of the code that you are running. As long as it compiles, the specific files the tool generates isn't so crucial.

  2. This is a compilation error. When you do go install foo it will try to build the foo package as an executable and then move that to your GOPATH/bin directory. It seems that the generated code in restapi/configure_todo_list.go isn't correct for the operations code generated.

  3. All you need to run this tutorial yourself is an empty directory and the swagger tool (not its source code). You run the commands from the root of this empty project. In order not to run into GOPATH problems I would initialise a module with go mod init todo-list-example before doing anything else. Note that while the todo-list example code exists inside the go-swagger source, it's there just for documenting example usage and output.

What I would advice for #2 is to make sure you're using a properly released version of go-swagger, rather than installing from the latest commit (which happens when you just do a go get), as I have found that to be occasionally unstable.

Next, re-generate the entire server, but make sure you also regenerate restapi/configure_todo_list.go by passing --regenerate-configureapi to your swagger generate call. This file isn't always refreshed because you're meant to modify it to configure your app, and if you changed versions of the tool it may be different and incompatible.

If after that you still get the compilation error, it may be worth submitting a bug report at https://github.com/go-swagger/go-swagger/issues.

Ezequiel Muns
  • 7,492
  • 33
  • 57
  • For #3, to clarify, any empty directory (not necessarily inside `$GOPATH`) will work right? – AEWRocks Jan 07 '21 at 03:15
  • If you're outside gopath, it's best to initialise a module with `go mod init` before you do anything else. If inside gopath is should just work. – Ezequiel Muns Jan 07 '21 at 11:22
0

Thanks @EzequielMuns. The errors in #2 went away after I ran go get - u -f ./... as stated in

...
For this generation to compile you need to have some packages in your GOPATH:

        * github.com/go-openapi/runtime
        * github.com/jessevdk/go-flags

You can get these now with: go get -u -f ./...
AEWRocks
  • 157
  • 1
  • 2
  • 10
0

I think it's an error of swagger code generation. You can do as folloing to fix this:

  • delete file configure_todo_list.go;
  • regenerate code.
# swagger generate server -A todo-list -f ./swagger.yml

Then, you can run command go install ./cmd/todo-list-server/, it will succeed.

styshoo
  • 563
  • 4
  • 9