gin框架swagger的使用的坑

1. go get -u github.com/swaggo/swag/cmd/swag

下载这个包的时候可能会遇到这种情况:dial tcp 216.239.37.1:443: i/o timeout,如图
在这里插入图片描述

package golang.org/x/text/unicode/bidi: unrecognized import path "golang.org/x/text/unicode/bidi" (https fetch: Get https://golang.org/x/text/unicode/bidi?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
package golang.org/x/text/unicode/norm: unrecognized import path "golang.org/x/text/unicode/norm" (https fetch: Get https://golang.org/x/text/unicode/norm?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)
package golang.org/x/text/width: unrecognized import path "golang.org/x/text/width" (https fetch: Get https://golang.org/x/text/width?go-get=1: dial tcp 216.239.37.1:443: i/o timeout)

重要的信息是这个
https://golang.org/x/text/
获取这个包失败,原因大家都懂,那么可以用另外一种方法获取这个包

cd $GOPATH/src/golang.org/x/
git clone https://github.com/golang/text.git

然后继续

go get -u github.com/swaggo/swag/cmd/swag

看看是否能正常下载,如果不能,按照上面的方法继续下载缺失的包
然后继续go get

2. 404 page not found

看的博客上少了一步,其实应该在router.go(路由文件)中添加

r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

3. Failed to load spec

gin框架swagger的使用的坑_第1张图片
如果浏览器显示Failed to load spec,你应该在控制台能看到报错:

2019/11/12 18:28:41 [Recovery] 2019/11/12 - 18:28:41 panic recovered:
GET /swagger/doc.json HTTP/1.1
Host: 127.0.0.1:8080
Accept: application/json,*/*
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8
Connection: keep-alive
Referer: http://127.0.0.1:8080/swagger/index.html
Sec-Fetch-Mode: cors
Sec-Fetch-Site: same-origin
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36


not yet registered swag

not yet registered swag
这是因为生成的docs文件夹没有被导入
在router.go(路由文件)中导入docs

_ "github.com/molefuckgo/gin-blog/docs"

你可能感兴趣的:(Go)