• Golang Gin 项目使用 Swagger


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

  • 相关阅读:
    3秒后按按钮
    成为编程高手的八大奥秘
    实现页面的简繁体的切换
    推荐优秀的css的编辑器
    Javascript 常用方法示例
    CSS 滤镜属性(图片透明)
    javaScript 验证
    JavaScript中substr和substring的区别
    setInterval和clearInterval 讲解
    TBody 的解释及用法
  • 原文地址:https://www.cnblogs.com/jehorn/p/9809751.html
Copyright © 2020-2023  润新知