首页 > 其他 > 详细

Golang Gin 项目使用 Swagger

时间:2018-10-18 13:04:50      阅读:327      评论:0      收藏:0      [点我收藏+]

Golang Gin 项目使用 Swagger

标签(空格分隔): Go


首先需要github.com/swaggo/gin-swaggergithub.com/swaggo/gin-swagger/swaggerFiles(参见gin-swagger)。

然后根据 github.com/swaggo/swag/cmd/swag文档获取到swag工具;执行swag init在项目根目录下生成docs文件夹。然后在路由中import _ "/docs"。这时候编译程序,打开http://localhost:8080/swagger/index.html就可以看到API。有时候打开页面js报错,多刷新几次就有了(原因未知)。

注意项目目录下,包依赖不能用src等名称,请看以下源码:

//exclude vendor folder
if ext := filepath.Ext(path); ext == ".go" &&
    !strings.Contains(string(os.PathSeparator)+path, string(os.PathSeparator)+"vendor"+string(os.PathSeparator)) &&
    !strings.Contains(string(os.PathSeparator)+path, string(os.PathSeparator)+".history"+string(os.PathSeparator)) &&
    !strings.Contains(string(os.PathSeparator)+path, string(os.PathSeparator)+".idea"+string(os.PathSeparator)) &&
    !strings.Contains(string(os.PathSeparator)+path, string(os.PathSeparator)+".git"+string(os.PathSeparator)) {
    fset := token.NewFileSet() // positions are relative to fset
    astFile, err := goparser.ParseFile(fset, path, nil, goparser.ParseComments)

swag工具排除了vendor.history.idea.git目录下的文件(参见pl)。可以使用godep等工具将依赖包放在vendor目录下。

Golang Gin 项目使用 Swagger

原文:https://www.cnblogs.com/jehorn/p/9809751.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!