加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

Golang Gin 项目使用 Swagger

发布时间:2020-12-16 09:33:10 所属栏目:大数据 来源:网络整理
导读:Golang Gin 项目使用 Swagger 标签(空格分隔): Go 首先需要 github.com/swaggo/gin-swagger 和 github.com/swaggo/gin-swagger/swaggerFiles (参见gin-swagger)。 然后根据 github.com/swaggo/swag/cmd/swag文档获取到swag工具;执行 swag init 在项目

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目录下。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读