go swag常用注释

生成在线文档校验接口时,主要使用这个@Param这种注解写对。个人理解,这些注释不是对代码功能的约束,而是对生成的前端测试ui中前端代码的约束。或者说对文档的约束

param的格式:
param name,param type,data type,is mandatory?,comment attribute(optional)

@Param enumstring query string false "string enums" Enums(A, B, C)

Param Type

  • query
  • path
  • header
  • body
  • formData

Data Type

  • string (string)
  • integer (int, uint, uint32, uint64)
  • number (float32)
  • boolean (bool)
  • user defined struct

Mime Types

主要在@accept【前端需要传哪个】、@produce【自己传给前端的是哪种】中使用

AliasMIME Type
jsonapplication/json
xmltext/xml
plaintext/plain
htmltext/html
mpfdmultipart/form-data
x-www-form-urlencodedapplication/x-www-form-urlencoded
json-apiapplication/vnd.api+json
json-streamapplication/x-json-stream
octet-streamapplication/octet-stream
pngimage/png
jpegimage/jpeg
gifimage/gif

值的数据约束

Field NameTypeDescription
validatestringDetermines the validation for the parameter. Possible values are: required.
default*Declares the value of the parameter that the server will use if none is provided, for example a “count” to control the number of results per page might default to 100 if not supplied by the client in the request. (Note: “default” has no meaning for required parameters.) See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-6.2. Unlike JSON Schema this value MUST conform to the defined type for this parameter.
maximumnumberSee https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.2.
minimumnumberSee https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.1.3.
maxLengthintegerSee https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.2.1.
minLengthintegerSee https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.2.2.
enums[*]See https://tools.ietf.org/html/draft-fge-json-schema-validation-00#section-5.5.1.
formatstringThe extending format for the previously mentioned type. See Data Type Formats for further details.
collectionFormatstringDetermines the format of the array if type array is used. Possible values are:csv - comma separated values foo,bar.ssv - space separated values foo bar.tsv - tab separated values foo\tbar.pipes - pipe separated values foo|bar.multi - corresponds to multiple parameter instances instead of multiple values for a single instance foo=bar&foo=baz. This is valid only for parameters in “query” or “formData”.Default value is csv.
// ShowAccount godoc
// @Summary Show a account
// @Description get string by ID
// @ID get-string-by-int
// @Accept  json
// @Produce  json
// @Param id path int true "Account ID"
// @Success 200 {object} model.Account
// @Header 200 {string} Token "qwerty"
// @Failure 400,404 {object} httputil.HTTPError
// @Failure 500 {object} httputil.HTTPError
// @Failure default {object} httputil.DefaultError
// @Router /accounts/{id} [get]
func (c *Controller) ShowAccount(ctx *gin.Context) {
	id := ctx.Param("id")
	aid, err := strconv.Atoi(id)
	if err != nil {
		httputil.NewError(ctx, http.StatusBadRequest, err)
		return
	}
	account, err := model.AccountOne(aid)
	if err != nil {
		httputil.NewError(ctx, http.StatusNotFound, err)
		return
	}
	ctx.JSON(http.StatusOK, account)
}

// ListAccounts godoc
// @Summary List accounts
// @Description get accounts
// @Accept  json
// @Produce  json
// @Param q query string false "name search by q"
// @Success 200 {array} model.Account
// @Header 200 {string} Token "qwerty"
// @Failure 400,404 {object} httputil.HTTPError
// @Failure 500 {object} httputil.HTTPError
// @Failure default {object} httputil.DefaultError
// @Router /accounts [get]
func (c *Controller) ListAccounts(ctx *gin.Context) {
	q := ctx.Request.URL.Query().Get("q")
	accounts, err := model.AccountsAll(q)
	if err != nil {
		httputil.NewError(ctx, http.StatusNotFound, err)
		return
	}
	ctx.JSON(http.StatusOK, accounts)
}

官方文档:
https://github.com/swaggo/swag#mime-types

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Swag是一个用于生成和管理Golang项目的Swagger文档的开源工具。首先,我们需要在项目的根目录下执行一些安装命令来安装Swag的相关库。具体命令如下: ``` $ go get -u github.com/swaggo/swag/cmd/swag $ go get -u github.com/swaggo/gin-swagger $ go get -u github.com/swaggo/files $ go get -u github.com/alecthomas/template ``` 安装完成后,我们需要重新启动服务端,并在浏览器中访问Swagger的地址(通常是http://127.0.0.1:8000/swagger/index.html),就可以看到项目的Swagger文档展示。这个文档主要分为三个部分:项目主体信息、接口路由信息和模型信息。它们共同组成了Swagger文档的主要内容。 Swag的使用可以参考官方提供的快速上手文档和详细使用指南。这些文档将帮助您了解如何在Golang项目中使用Swag生成和管理Swagger文档。 值得注意的是,Swag的使用涉及到Golang和JavaScript两个部分。因此,在学习和使用Swag时,可以参考一些相关文章和教程,以更好地理解和运用其中的技术点。例如,可以了解关于Golang注释解析的内容,以及如何修正Swag与logrus以及go ast的集成。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [基于golang的swagger超贴心、超详细使用指南【有很多坑】](https://blog.csdn.net/qq_57467091/article/details/123373790)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [Golang OpenAPI工具Swag修正——go ast篇](https://blog.csdn.net/baijiafan/article/details/125673803)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值