doushou3814
doushou3814
采纳率100%
2016-06-01 00:17

使用golang注释生成Swagger规范

已采纳

我正在尝试使用Swagger为用Golang编写的RESTAPI创建文档。我一直遵循这个指南https://goswagger.io/generate/spec.html 来做这一点,并已经能够根据我在主文件中的注释创建json文件,但我不知道如何创建转换为实际swagger规范的.yaml文件。我遵循了页面上的说明,并在我的主文件中包含了//go:GenerateswaggerGeneratespec。有人知道如何创建.yaml文件吗?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • doubo4336 doubo4336 5年前

    The generated spec would be json not yaml at this stage, not saying it can't be made to do yaml but currently it only generates json.

    you can annotate your main package with: //go:generate swagger generate spec -o swagger.json

    then you can generate the spec with: go generate when you are in folder of the main package. If your main package is not your project root but something like $project_root/cmd/my-server then you can run go generate ./cmd/my-server from the project root.

    点赞 评论 复制链接分享
  • dopuzf0898 dopuzf0898 2年前

    Yes, you can create a YAML file. Following an example.

    swagger generate spec -m -o ../../api/swagger.yaml
    

    Also, feel free to check the Blog Post.

    点赞 评论 复制链接分享