gin请求数据绑定至结构体
form表单数据绑定结构体
var b struct
c.Bind(&b)
package main
import "github.com/gin-gonic/gin"
/**
gin数据绑定:
form表单数据绑定到结构体
*/
type StructA struct {
FieldA string `form:"field_a"`
}
type StructB struct {
NestedStruct StructA
FieldB string `form:"field_b"`
}
type StructC struct {
NestedStructPointer *StructA
FieldC string `form:"field_c"`
}
type StructD struct {
NestedAnonyStruct struct {
FieldX string `form:"field_x"`
}
FieldD string `form:"field_d"`
}
func GetDataB(c *gin.Context) {
var b StructB
c.Bind(&b)
c.JSON(200, gin.H{
"a": b.NestedStruct,
"b": b.FieldB,
})
}
func GetDataC(c *gin.Context) {
var b StructC
c.Bind(&b)
c.JSON(200, gin.H{
"a": b.NestedStructPointer,
"c": b.FieldC,
})
}
func GetDataD(c *gin.Context) {
var b StructD
c.Bind(&b)
c.JSON(200, gin.H{
"x": b.NestedAnonyStruct,
"d": b.FieldD,
})
}
func main() {
r := gin.Default()
//request: http://localhost:8080/getb?field_a=hello&field_b=world
//result: {"a":{"FieldA":"hello"},"b":"world"}
r.GET("/getb", GetDataB)
//request: http://localhost:8080/getc?field_a=hello&field_c=world
//result: {"a":{"FieldA":"hello"},"c":"world"}
r.GET("/getc", GetDataC)
//request: http://localhost:8080/getd?field_x=hello&field_d=world
//result: {"d":"world","x":{"FieldX":"hello"}}
r.GET("/getd", GetDataD)
r.Run()
}
checkbox复选框数据绑定结构体
var b struct
c.ShouldBind(&b)
main.go
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
type myForm struct {
Colors []string `form:"color[]"`
}
func formHandler(c *gin.Context){
var f myForm
c.ShouldBind(&f)
c.JSON(http.StatusOK,gin.H{
"color":f.Colors,
})
}
func Index(c *gin.Context){
c.HTML(http.StatusOK,"index.html",nil)
}
func main(){
r := gin.Default()
//全局加载模板路径
r.LoadHTMLGlob("*")
r.GET("/",Index)
r.POST("/commit",formHandler)
r.Run()
}
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>checkbox多数据绑定</title>
</head>
<body>
<form method="post" action="/commit">
red:<input type="checkbox" name="color[]" value="red"/><br>
green:<input type="checkbox" name="color[]" value="green"/><br>
blue:<input type="checkbox" name="color[]" value="blue"/><br>
yellow:<input type="checkbox" name="color[]" value="yellow"/><br>
<input type="submit" value="commit"/>
</form>
</body>
</html>
get,post请求数据绑定
c.BindQuery(&user)
c.BindJSON(&user)
package main
import (
"github.com/gin-gonic/gin"
"net/http"
"time"
)
/**
Bind query string or post data
get,post请求数据绑定
*/
type User struct {
Username string `form:"username" json:"username"`
Age int `form:"age" json:"age"`
Birthday time.Time `form:"birthday" time_format:"2006-01-02" time_utc:"1" json:"birthday"`
}
func handlerGet(c *gin.Context){
var user User
err := c.BindQuery(&user)
if err==nil {
c.JSON(http.StatusOK,user)
}else {
c.String(http.StatusBadRequest,"绑定失败"+err.Error())
}
}
func handlerPost(c *gin.Context){
var user User
err := c.BindJSON(&user)
if err==nil {
c.JSON(http.StatusOK,user)
}else {
c.String(http.StatusBadRequest,"绑定失败"+err.Error())
}
}
func main(){
r := gin.Default()
//request : http://localhost:8080/get?username=ztind&age=18&birthday=1999-10-10
//result: {"Username":"ztind","Age":18,"Birthday":"1999-10-10T00:00:00Z"}
r.GET("/get",handlerGet)
r.POST("/post",handlerPost)
r.Run()
}
GET:
request:http://localhost:8080/get?username=ztind&age=18&birthday=1999-10-10
result:
{"username":"jimmy","age":18,"birthday":"1999-10-10T00:00:00Z"}
POST:http://localhost:8080/post
request body:
注意:time.Time类型字段需拼接一个_str才能成功
https://studygolang.com/topics/9960/comment/36455
{
"username":"ztind",
"age":18,
"birthday_str":"2015-01-15"
}
uri绑定
c.ShouldBindUri(&person)
binding:“required” 此字段必填
binding:“required,uuid” 此字段必填且uuid类型
package main
import (
"github.com/gin-gonic/gin"
"net/http"
)
/**
绑定uri:统一资源标识符
*/
type Person struct {
ID string `uri:"id" binding:"required,uuid"`
Name string `uri:"name" binding:"required"`
}
func main(){
r:=gin.Default()
r.GET("/:name/:id", func(c *gin.Context) {
var person Person
if err:=c.ShouldBindUri(&person);err!=nil{
c.JSON(http.StatusBadRequest,gin.H{"msg":err})
return
}
c.JSON(http.StatusOK,person)
})
r.Run()
}
测试:
localhost:8080/thinkerou/987fbc97-4bed-5078-9f07-9141ba07c9f3
localhost:8080/thinkerou/not-uuid