Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the implementation of parameter binding in Gin framework

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

Gin framework parameter binding is how to achieve, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

In order to obtain the relevant parameters of the request more conveniently and improve the development efficiency, we can identify the request data type based on the request Content-Type and use the reflection mechanism to automatically extract the QueryString, form form, JSON, XML and other parameters into the structure. The following sample code demonstrates the power of .ShouldBind (), which automatically extracts data of type JSON, form form, and QueryString based on requests, and binds values to specified structure objects.

The following example binds parameters to QueryString, form, and json, respectively.

Note:

1. Uppercase attribute names in the body of the structure

2. Add address character:

Code:

Package mainimport ("github.com/gin-gonic/gin"net/http") type User struct {Username string `json: "user" form: "userform" `Password string `json: "pwd" form: "pwdform" `} func main () {r: = gin.Default () var u User / / queryString data binding r.GET ("/ user") Func (c * gin.Context) {/ / Parameter binding err: = c.ShouldBind (& u) if err = = nil {c.JSON (http.StatusOK, gin.H {"user": u.Username, "pwd": u.Password })}) / / form form data binding r.POST ("/ form", func (c * gin.Context) {/ / Parameter binding err: = c.ShouldBind (& u) if err = = nil {c.JSON (http.StatusOK) Gin.H {"user": u.Username, "pwd": u.Password,})}) / / form form data binding r.POST ("/ json" Func (c * gin.Context) {/ / Parameter binding err: = c.ShouldBind (& u) if err = = nil {c.JSON (http.StatusOK, gin.H {"user": u.Username, "pwd": u.Password }) r.Run (": 9090")}

Debug:

The running results are simulated using postman, which are:

The result of parameter binding to querystring:

The result of parameter binding to the form form:

The result of parameter binding to json:

Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report