使用golang注释生成Swagger规范 [英] Generating Swagger spec with golang comments

查看:1252
本文介绍了使用golang注释生成Swagger规范的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Swagger为用golang编写的REST API创建文档.我一直在遵循此指南: https://goswagger.io/generate/spec.html 至这样做,已经能够从主文件中的注释创建json文件,但是我不知道如何创建将转换为实际swagger规范的.yaml文件.我已按照页面上的说明进行操作,并在主文件中包含了//go:generate swagger generate spec.有人知道如何创建.yaml文件吗?

解决方案

在此阶段,生成的规范将是json而不是yaml,不是说它不能被制成yaml,而是目前仅生成json. >

您可以使用以下内容注释主软件包://go:generate swagger generate spec -o swagger.json

然后,当您在主软件包的文件夹中时,可以使用以下命令生成规格:go generate.如果您的主软件包不是项目根目录,而是$project_root/cmd/my-server之类的东西,那么您可以从项目根目录运行go generate ./cmd/my-server.

I am trying to create documentation for my REST API written in golang using Swagger. I have been following this guide: https://goswagger.io/generate/spec.html to do so and have been able to create the json file from my comments in my main file, however I do not know how to create the .yaml file that would translate to the actual swagger spec. I have followed the instructions on the page and included the //go:generate swagger generate spec in my main file. Anyone know how to create the .yaml file?

解决方案

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.

这篇关于使用golang注释生成Swagger规范的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆