1、go get -u github.com/swaggo/swag/cmd/swag
$ go get -u github.com/swaggo/swag/cmd/swag
报错:go 获取golang.org/x/tools包失败
go get golang.org/x/tools/go/packages
package golang.org/x/tools/go/packages: unrecognized import path "golang.org/x/tools/go/packages" (https fetch: Get https://golang.org/x/tools/go/packages?go-get=1: proxyconnect tcp: net/http: TLS handshake timeout)
替代的解决办法就是直接去https://github.com/golang/tools.git 克隆一份然后放到E:\gopath\src[golang.org](http://golang.org/)\x\tools目录下
2、安装 gin-swagger
$ go get -u github.com/swaggo/gin-swagger
$ go get -u github.com/swaggo/gin-swagger/swaggerFiles
3、编写API注释
4、生成
进入项目根目录
[$ gin-blog]# swag init
2018/03/13 23:32:10 Generate swagger docs....
2018/03/13 23:32:10 Generate general API Info
2018/03/13 23:32:10 create docs.go at docs/docs.go
坑爹的出现了下面的报错,然后懵逼了,从来没遇到过,后来仔细看了下报错的原因,去把对应目录下得testdata删掉了。然后就成功了。
ParseFile panic:golang.org\x\tools\cmd\fiximports\testdata\s rc\old.com\bad\bad.go:2:43: expected 'package', found 'EOF'
panic: ParseFile panic:golang.org\x\tools\cmd\fiximports\testdata\src\old.com\ba d\bad.go:2:43: expected 'package', found 'EOF'
有疑问加站长微信联系(非本文作者)