Gin 学习之绑定参数

简介: Gin 学习之绑定参数

01

概念


要将请求体绑定到结构体中,需要使用模型绑定。Gin 目前支持JSON、XML、YAML和标准表单值的绑定(foo=bar&boo=baz)。


使用时,结构体字段首字母必须大写。需要在要绑定的所有字段上,设置相应的tag。例如,使用 JSON 绑定时,设置字段标签为 json:"fieldname"。你也可以指定必须绑定的字段。如果一个字段的 tag 加上了 binding:"required",但绑定时是空值, Gin 会报错。Gin 使用 go-playground/validator.v8 进行验证。


Gin 提供了两类绑定方法:Must bind 和 Should bind。


Must bind 的方法有 Bind,BindJSON,BindXML,BindQuery,BindYAML,这些方法属于 BindWith 的具体调用。


Must bind 如果发生绑定错误,则请求终止,并触发 c.AbortWithError(400, err).SetType(ErrorTypeBind)。响应状态码被设置为 400 并且 Content-Type 被设置为 text/plain; charset=utf-8。如果您在此之后尝试设置响应状态码,Gin会输出日志 [GIN-debug] [WARNING] Headers were already written. Wanted to override status code 400 with 422。

Should bind的方法有 ShouldBind,ShouldBindJSON,ShouldBindXML,ShouldBindQuery,ShouldBindYAML,这些方法属于 ShouldBindWith 的具体调用。


Should bind 如果发生绑定错误,Gin 会返回错误并由开发者处理错误和请求。


使用 Bind 方法时,Gin 会尝试根据 Content-Type 推断如何绑定。如果你明确知道要绑定什么,可以使用 MustBindWith 或 ShouldBindWith。


如果您希望更好地控制绑定,考虑使用 ShouldBind 等效方法。如果对 Must bind 的方法感兴趣,可以查阅文档或阅读 Gin 源码,本文我们主要介绍 Should bind 的方法。


本文示例代码需要使用到的自定义结构体类型。

type user struct {
  Name string `form:"username" json:"username" xml:"username" uri:"username"`
  Age  int    `form:"age" json:"age" xml:"age" uri:"age"`
}
type student struct {
  UserInfo user `form:"userinfo"`
  Score    int  `form:"score" json:"score" xml:"score" uri:"score"`
}


02

ShouldBind



func (*gin.Context).ShouldBind(obj interface{}) error


ShouldBind 支持绑定 urlencoded form 和 multipart form


如果是 `GET` 请求,只使用 `Form` 绑定引擎(`query`)。


如果是 `POST` 请求,首先检查 `content-type` 是否为 `JSON` 或 `XML`,然后再使用 `Form`(`form-data`)。


绑定到结构体

640.png

绑定到嵌套结构体

640.png


03

ShouldBindJSON



func (*gin.Context).ShouldBindJSON(obj interface{}) error


ShouldBindJSON 是 for c.ShouldBindWith(obj, binding.JSON) 的简写方式。


示例代码:

640.png


04

ShouldBindXML



func (*gin.Context).ShouldBindXML(obj interface{}) error

ShouldBindXML 是 c.ShouldBindWith(obj, binding.XML) 的简写方式。


示例代码:

640.png


05

ShouldBindQuery



func (*gin.Context).ShouldBindQuery(obj interface{}) error

ShouldBindQuery 是 c.ShouldBindWith(obj, binding.Query) 的简写方式。


ShouldBindQuery 如果 url 查询参数和 post 数据都存在,函数只绑定 url 查询参数而忽略 post 数据。


示例代码:

640.png


06

ShouldBindYAML



func (*gin.Context).ShouldBindYAML(obj interface{}) error

ShouldBindYAML 是 c.ShouldBindWith(obj, binding.YAML) 的简写方式。

示例代码:

640.png


07

ShouldBindWith



func (*gin.Context).ShouldBindWith(obj interface{}, b binding.Binding) error

ShouldBindWith 使用指定的绑定引擎,绑定传递过来的结构体指针。


示例代码:

640.png


08

ShouldBindUri



func (*gin.Context).ShouldBindUri(obj interface{}) error

ShouldBindUri binds the passed struct pointer using the specified binding engine.


示例代码:

640.png





目录
相关文章
|
3月前
|
Web App开发 监控 安全
OSS客户端签名直传实践:Web端安全上传TB级文件方案(含STS临时授权)
本文深入解析了客户端直传技术,涵盖架构设计、安全机制、性能优化等方面。通过STS临时凭证与分片上传实现高效安全的文件传输,显著降低服务端负载与上传耗时,提升系统稳定性与用户体验。
422 2
|
Ubuntu Linux 时序数据库
|
存储 JSON 前端开发
gin框架学习笔记(四) ——参数绑定与参数验证
gin框架学习笔记(四) ——参数绑定与参数验证
416 0
|
7月前
|
人工智能 算法 API
谷歌AI Gemini 2.5 pro国内使用教程, 2025最新版!
在 2025 年 2 月初,谷歌又推出了 Gemini 2.0 Pro 系列模型,进一步巩固了其在 AI 领域的领先地位,同时也正式向外界宣告,我们进入了 Gemini 2.0 时代
3400 5
|
12月前
|
Go
go语言李mapstructure啥意思
go语言李mapstructure啥意思
|
存储 设计模式 前端开发
MVC架构和DDD架构的区别?
最近在学习一个开源社区项目,第一次听说了DDD项目架构,于是通过搜索之后来分享给大家
|
12月前
|
数据采集 Linux 网络安全
python 爬虫遇到的aiohttp证书错误解决办法
python 爬虫遇到的aiohttp证书错误解决办法
581 0
|
Unix 程序员 Go
5分钟撸一个时间转换器,Go语言教你如何开挂
5分钟撸一个时间转换器,Go语言教你如何开挂
247 0
|
监控 关系型数据库 数据库
关系型数据库考虑索引的选择性
【5月更文挑战第20天】
247 4
|
JSON Java Linux
知识分享之Golang——Gin学习之context上下文的获取与使用(三)
知识分享之Golang篇是我在日常使用Golang时学习到的各种各样的知识的记录,将其整理出来以文章的形式分享给大家,来进行共同学习。欢迎大家进行持续关注。 知识分享系列目前包含Java、Golang、Linux、Docker等等。
725 0
知识分享之Golang——Gin学习之context上下文的获取与使用(三)