Introduction
In the Gin
framework, parameters can receive a key by designation and are directly bound to the structure. Let’s explain how it happens:
Binding method
Two types of binding methods exist in the Gin
framework, Must bind
and Should bind
, and the corresponding methods of these two types are as follows:
Function |
Must bind method |
Should bind method |
|
Bind |
ShouldBind |
JSON bind |
BindJSON |
ShouldBindJSON |
XML bind |
BindXML |
ShouldBindXML |
GET bind |
BindQuery |
ShouldBindQuery |
YAML bind |
BindYAML |
ShouldBindYAML |
MustBindWith and ShouldBindWith
Bind*
A type’s method is an MustBindWith
encapsulation; Should*
a type’s method is ShouldBindWith
an encapsulation;
Define the bound structure
Label Description:
form:"paramName"
: paramName is the name of the parameter;
binding:"required"
: means that the field must be bound;
Bind the uri parameter
By using the function BindQuery
and ShouldBindQuery
, used to bind only GET
the parameters in the request uri
, such as: /funcName?a=x&b=x.
Bind JSON
Use functions BindJSON
and ShouldBindJSON
to bind submitted JSON
parameter information.
Binding XML
Use functions BindXML
and ShouldBindXML
to bind submitted XML
parameter information.
Binding request.Body
c.Request.Body
Cannot be called multiple times, after the first binding c.Request.Body
will be set to EOF.