In the previous articles, the binding of data or parameters needs to be bound one by one, such as this:
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
//建立一个结构体来存储数据
type UserInfo struct {
username string
password string
}
func main() {
r := gin.Default()
r.GET("/user", func(c *gin.Context) {
username := c.Query("username")
passsword := c.Query("password")
u := UserInfo{
username: username,
password: passsword,
}
fmt.Printf("%v\n", u)
//给这个请求返回一个JSON数据
c.JSON(http.StatusOK, gin.H{
"message": "ok",
})
})
r.Run(":9090")
}
The result is as follows:
But a single parameter is a little more, such a one-by-one binding is too troublesome. Here is the ShouldBind() in the Gin framework, which is used to bind the parameters carried by the request with the back-end structure. For example, we have The UserInfo structure has two fields, username and password. If these two fields appear in the request, ShouldBind() will automatically take out these two values for us, and then accompany us to initialize a structure, and we can get a UserInfo variable of type.
The use of ShouldBind() requires attention:
- ShouldBind receives the address (& object name) of the structure object, not the object
- The first letter of each field of the structure should be capitalized (similar to Java public declaration)
- The structure should be tagged, the json tag should be tagged when sending a request in json format, and the form tag should be tagged when sending a request in the address bar.
ShouldBind mock queryString
For example: if you want to put
<http://127.0.0.1:9090/user?Username=bill&Password=111111123>
The two parameters of this link are taken and can be written as follows:
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
//建立一个结构体来存储数据
type UserInfo struct {
Username string
Password string
}
func main() {
r := gin.Default()
r.GET("/user", func(c *gin.Context) {
//username := c.Query("username")
//passsword := c.Query("password")
//u := UserInfo{
// username: username,
// password: passsword,
//}
//声明一个UserInfo类型的变量u
var u UserInfo
//这里把地址传过去
err := c.ShouldBind(&u)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
} else {
fmt.Printf("%#v\n", u)
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
}
})
r.Run(":9090")
}
The result is as follows:
ShouldBind emulates PostForm
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
//建立一个结构体来存储数据
type UserInfo struct {
Username string
Password string
}
func main() {
r := gin.Default()
r.GET("/user", func(c *gin.Context) {
//username := c.Query("username")
//passsword := c.Query("password")
//u := UserInfo{
// username: username,
// password: passsword,
//}
//声明一个UserInfo类型的变量u
var u UserInfo
//这里把地址传过去
err := c.ShouldBind(&u)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
} else {
fmt.Printf("%#v\n", u)
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
}
})
r.POST("/form", func(c *gin.Context) {
var u UserInfo
//这里把地址传过去
err := c.ShouldBind(&u)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
} else {
fmt.Printf("%#v\n", u)
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
}
})
r.Run(":9090")
}
Use APIPOST or POSTMAN to mock the data in the body of the POST request:
The result in the terminal is as follows:
ShouldBind simulates binding JSON data (it is often used in the future, because most of the front and back ends are separated in the future, and JSON data needs to be bound in this way)
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"net/http"
)
//建立一个结构体来存储数据
type UserInfo struct {
Username string
Password string
}
func main() {
r := gin.Default()
r.GET("/user", func(c *gin.Context) {
//username := c.Query("username")
//passsword := c.Query("password")
//u := UserInfo{
// username: username,
// password: passsword,
//}
//声明一个UserInfo类型的变量u
var u UserInfo
//这里把地址传过去
err := c.ShouldBind(&u)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
} else {
fmt.Printf("%#v\n", u)
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
}
})
r.POST("/form", func(c *gin.Context) {
var u UserInfo
//这里把地址传过去
err := c.ShouldBind(&u)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
} else {
fmt.Printf("%#v\n", u)
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
}
})
r.POST("/json", func(c *gin.Context) {
var u UserInfo
//这里把地址传过去
err := c.ShouldBind(&u)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
} else {
fmt.Printf("%#v\n", u)
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
}
})
r.Run(":9090")
}
The data mocked with APIPOST is as follows:
The terminal results are as follows:
In fact, observe the code of the above three parts, whether it is the binding of QueryString, PostForm or JSON data, the code of this part is exactly the same:
var u UserInfo
//这里把地址传过去
err := c.ShouldBind(&u)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{
"error": err.Error(),
})
} else {
fmt.Printf("%#v\n", u)
c.JSON(http.StatusOK, gin.H{
"status": "ok",
})
}
Note that the shouBind() method can be processed in different ways according to the different types of contentType in the request.
Reference: bilibili
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。