码迷,mamicode.com
首页 > 其他好文 > 详细

Golang Gin 项目使用 Swagger

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

标签:token   注意   nil   comm   原因   his   local   new   mit   

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

标签:token   注意   nil   comm   原因   his   local   new   mit   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!