8

I am new to go-lang and so to go-swagger. I am following a blog and have installed go-swagger with command :

go get -u github.com/go-swagger/go-swagger/cmd/swagger

I can see that the go-swagger folder is created in

C:\Go\bin\src\github.com\go-swagger

Now, I added my project path to $GOPATH :

echo %GOPATH%
C:\Go\bin;D:\Personal\Learning\GoLang\Project-2;D:\Personal\Learning\GoLang\swagger;

When I run

D:\Personal\Learning\GoLang\swagger>swagger ./swagger.yaml
'swagger' is not recognized as an internal or external command,
operable program or batch file.

What am I missing ? Also, I would be grateful if you can suggest me some good material for go-swagger as I am finding it very difficult to setup everything. There arent much blogs which can help me do HELLO WORLD kinda setup

Thanks

Update 1:

I tried to set GOBIN but no luck with that:

D:\Personal\Learning\GoLang\swagger>swagger validate https://raw.githubusercontent.com/swagger-api/swagger-spec/master/examples/v2.0/json/petstore-expanded.json
'swagger' is not recognized as an internal or external command,
operable program or batch file.

D:\Personal\Learning\GoLang\swagger>echo %GOBIN%
C:\Go\bin\;

Update 2:

I tried absolute path as suggested as well, but no luck:

D:\Personal\Learning\GoLang\swagger>C:\Go\bin\swagger validate https://raw.githubusercontent.com/swagger-api/swagger-spec/master/examples/v2.0/json/petstore-expanded.json
'C:\Go\bin\swagger' is not recognized as an internal or external command,
operable program or batch file.

Update 3:

Below command worked for me but it doesn't seem to be proper way:

go run C:\Go\bin\src\github.com\go-swagger\go-swagger\cmd\swagger\swagger.go  validate https://raw.githubusercontent.com/swagger-api/swagger-spec/master/examples/v2.0/json/petstore-expanded.json
Samuel
  • 1,128
  • 4
  • 14
  • 34

6 Answers6

6

Fix for Mac Users

The error should be something like this.

enter image description here

You should add %GOBIN% directory to the path.

Fix: Add this line to ~/.zshrc

export PATH=$PATH:$HOME/go/bin

Note: My go bin folder is in $HOME/go/bin If it is not the case with you use your custom path.

Restart the terminal once the above changes are made

Anjan Talatam
  • 2,212
  • 1
  • 12
  • 26
3

There are pre-build binary, you just download to any folder, add folder to system PATH to execute from any where. On windows, you can download https://github.com/go-swagger/go-swagger/releases/download/v0.27.0/swagger_windows_amd64.exe (rename the binary to swagger.exe, and save to C:\Users{your-user}\go\bin)

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. – Tyler2P Aug 18 '21 at 10:20
1

When you install swagger or any other Go binary, the executable is on the %GOBIN% directory. To call the swagger executable you need to add the %GOBIN% directory to the Windows Path not the GOPATH or call it using the absolute path.

D:\Personal\Learning\GoLang\swagger> C:\Go\bin\swagger ./swagger.yaml

To add the go binaries to the path look here https://stackoverflow.com/a/9546345/1199408.

georgeok
  • 5,321
  • 2
  • 39
  • 61
  • No luck with absolute path : `D:\Personal\Learning\GoLang\swagger>C:\Go\bin\swagger validate https://raw.githubusercontent.com/swagger-api/swagger-spec/master/examples/v2.0/json/petstore-expanded.json 'C:\Go\bin\swagger' is not recognized as an internal or external command, operable program or batch file.` . – Samuel Jun 05 '19 at 06:52
1

When you do go get somepackage/cmd/somecmd the executable is added, not to %GOBIN%, but rather to %GOPATH%/bin.

You must therefore add this directory to your path:

echo %GOPATH%
// make sure you have a nonempty GOPATH
setx PATH %PATH%;%GOPATH%/bin
Ezequiel Muns
  • 7,492
  • 33
  • 57
1

One way that worked for me on Windows:

git clone https://github.com/go-swagger/go-swagger

cd go-swagger

go install ./cmd/swagger

0

Assuming you already have GO installed on your machine. Also a valid swagger.yaml file with end-points details.

First, you need to install go-swagger. Next Mandatory thing, you would need have is go.mod. Generate one using following command.

go mod init ModuleName

And then try generating server with the following command :-

swagger generate server

Boom, this would generate Code with folders for server. Next try generating the client with following command :-

swagger generate client

That is it. Above steps are bare minimum steps you need to have a Go-rest project. Run docker-less local set up using main.go

Hope this helps!!