三、参数绑定

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

Binding数据
注意:后面的form: “userform”表示在form中这个字段是userform,不是Username, 同样json: “user”和uri: “uriuser”也是
注意:binding: “required”要求这个字段在client端发送的时候必须存在,否则报错!

stringBind

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package main

import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)

// 定义参数绑定的结构体
type UserInfo struct {
Username string `json:"user" form:"userform" uri:"uriuser" binding:"required"`
Password string `json:"pwd" form:"pwdform" uri:"uripwd" binding:"required"`
}

// stringbind路由方法
func stringBind(engine *gin.Engine) {
var u UserInfo
engine.GET("/sting-bind", func(c *gin.Context) {
err := c.ShouldBind(&u)
if err == nil {
c.JSON(http.StatusOK, gin.H{
"user": u.Username,
"passwd": u.Password,
})
}
})
}

func main() {
engine := gin.Default()
stringBind(engine)
engine.Run(":5656")
}

formBind

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package main

import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)

// 定义参数绑定的结构体
type UserInfo struct {
Username string `json:"user" form:"userform" uri:"uriuser" binding:"required"`
Password string `json:"pwd" form:"pwdform" uri:"uripwd" binding:"required"`
}


func formBind(engine *gin.Engine) {
var u UserInfo
engine.POST("/form-bind", func(c *gin.Context) {
err := c.ShouldBind(&u)
if err == nil {
c.JSON(http.StatusOK, gin.H{
"user": u.Username,
"passwd": u.Password,
})
}
})
}


func main() {
engine := gin.Default()
formBind(engine)

engine.Run(":5656")
}

PostMan提交结果

jsonBind

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package main

import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)

// 定义参数绑定的结构体
type UserInfo struct {
Username string `json:"user" form:"userform" uri:"uriuser" binding:"required"`
Password string `json:"pwd" form:"pwdform" uri:"uripwd" binding:"required"`
}

func jsonBind(engine *gin.Engine) {
var u UserInfo
engine.POST("/json-bind", func(c *gin.Context) {
err := c.ShouldBind(&u)
if err == nil {
c.JSON(http.StatusOK, gin.H{
"user": u.Username,
"passwd": u.Password,
})
}
})
}

func main() {
engine := gin.Default()
jsonBind(engine)
engine.Run(":5656")
}

PostMan提交结果

uriBind

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package main

import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)

// 定义参数绑定的结构体
type UserInfo struct {
Username string `json:"user" form:"userform" uri:"uriuser" binding:"required"`
Password string `json:"pwd" form:"pwdform" uri:"uripwd" binding:"required"`
}


func uriBind(engine *gin.Engine) {
engine.POST("/uri-bind/:uriuser/*uripwd", func(ctx *gin.Context) {
var u UserInfo
if err := ctx.ShouldBindUri(&u); err != nil {
fmt.Println(err)
ctx.String(http.StatusBadRequest, "参数解析错误")
} else {
ctx.JSON(200, gin.H{
"name": u.Username,
"pwd": u.Password,
})
}
})
}


func main() {
engine := gin.Default()
uriBind(engine)
engine.Run(":5656")
}

PostMan提交结果

四、数据返回

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main

import "github.com/gin-gonic/gin"

// 返回string数据处理器示例
func retString(engine *gin.Engine) {
engine.GET("/ret-string", func(c *gin.Context) {
c.String(200, "return string")
})
}

// 返回Json数据处理器示例
func retJson(engine *gin.Engine) {
engine.GET("/ret-json", func(c *gin.Context) {
c.JSON(200, gin.H{
"name": "zhangsan",
"msg": "ok",
})
})
}

// 返回Yml数据处理器示例
func retYml(engine *gin.Engine) {
engine.GET("/ret-yml", func(c *gin.Context) {
c.YAML(200, gin.H{
"name": "zhangsan",
"msg": "ok",
})
})
}

// 返回XML数据处理器示例
func retXml(engine *gin.Engine) {
engine.GET("/ret-xml", func(c *gin.Context) {
c.XML(200, gin.H{
"name": "zhangsan",
"msg": "ok",
})
})
}

func main() {
engine := gin.Default()

retString(engine)
retJson(engine)
retYml(engine)
retXml(engine)

engine.Run(":5656")
}

postman提交string返回

postman提交json返回

postman提交yml返回

postman提交xml返回