其他分享
首页 > 其他分享> > go swag常用注释

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

Data Type

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

标签:swag,string,err,ctx,注释,json,https,go,httputil
来源: https://blog.csdn.net/qq_42873554/article/details/118797414