Golang Gin 项目使用 Swagger

Golang Gin 项目使用 Swagger

标签(空格分隔): Gohtml


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

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

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

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

相关文章
相关标签/搜索