本教材由知了传课辛苦制作而成,仅供学习使用,请勿用于商业用途!如进行转载请务必注明出处!谢谢!

参数绑定

它能够基于请求自动提取JSON、form表单和QueryString类型的数据,并把值绑定到指定的结构体对象

一、ShouldBind

示例代码:

type User struct {
Id int form:"id" json:"id"
Name string form:"name" json:"name"

}

structTag:指定字段名称,不用使用首字母大写的

func Index(contxt *gin.Context) {

var u User
err := contxt.ShouldBind(&u)
fmt.Println(err)
fmt.Println(u)
contxt.String(http.StatusOK, “Hello %s”, u.Name)

}

二、ShouldBindWith

可以使用显式绑定声明绑定 multipart form:

c.ShouldBindWith(&form, binding.Form)

或者简单地使用 ShouldBind 方法自动绑定

三、ShouldBindQuery

ShouldBindQuery函数只绑定 url 查询参数而忽略 post 数据

1389人已阅读,今天你学习了吗?

添加新回复
目录