Gin框架
Gin框架 路由传值与或参数
##get请求传值 Query参数 - GET /user?uid=20&page=1 ```go router.GET("/user", func(c *gin.Context) { uid := c.Query("uid") page := c.DefaultQuery("page", "0") c.String(200, "uid=%v page=%v", uid, page) }) ``` ## 动态传值、Param获取参数 ```go func main() { router := gin.Default() // 此 handler 将匹配 /user/john 但不会匹配 /user/ 或者 /user router.GET("/user/:name", func(c *gin.Context) { name := c.Param("name") c.String(http.StatusOK, "Hello %s", name) }) // 此 handler 将匹配 /user/john/ 和 /user/john/send // 如果没有其他路由匹配 /user/john,它将重定向到 /user/john/ router.GET("/user/:name/*action", func(c *gin.Context) { name := c.Param("name") action := c.Param("action") message := name + " is " + action c.String(http.StatusOK, message) }) router.Run(":8080") } ``` ## Post请求传值、PostForm获取 ```go router.POST("/doAddUser", func(c *gin.Context) { username := c.PostForm("username") password := c.PostForm("password" age := c.DefaultPostForm("age", "20") //hobbys := c.PostFormMap("hobby") //hobbys := c.QueryArray("hobby") hobbys := c.PostFormArray("hobby") c.JSON(200, gin.H{ "usernmae": username, "password": password, "age": age, "hobbys":hobbys } }) ``` ## 获取 GET POST 传递的数据绑定到结构体 为了能够更方便的获取请求相关参数,提高开发效率,我们可以基于请求的 Content-Type识别请求数据类型并利用反射机制自动提取请求中 QueryString、form 表单、JSON、XML 等参数到结构体中。 下面的示例代码演示了.ShouldBind()强大的功能,它能够基于请求自动提取 JSON、form 表单和 QueryString 类型的数据,并把值绑定到指定的结构体对象。 ```go //结构体 注意首字母大写 type Userinfo struct { Username string `form:"username" json:"user"` Password string `form:"password" json:"password"` } ``` ##### Get 传值绑定到结构体 >/?username=zhangsan&password=123456 ```go router.GET("/", func(c *gin.Context) { var userinfo Userinfo if err := c.ShouldBind(&userinfo); err == nil { c.JSON(http.StatusOK, userinfo) } else { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) } }) ``` ##### Post 传值绑定到结构体 ```go router.POST("/doLogin", func(c *gin.Context) { var userinfo Userinfo if err := c.ShouldBind(&userinfo); err == nil { c.JSON(http.StatusOK, userinfo) } else { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) } }) ``` ## 获取 Post Xml 数据 在 API 的开发中,我们经常会用到 JSON 或 XML 来作为数据交互的格式,这个时候我们 可以在 gin 中使用 c.GetRawData()获取数据。 ```xml <?xml version="1.0" encoding="UTF-8"?> <article> <content type="string">我是张三</content> <title type="string">张三</title> </article> ``` ```go type Article struct { Title string `xml:"title"` Content string `xml:"content"` } router.POST("/xml", func(c *gin.Context) { b, _ := c.GetRawData() // 从 c.Request.Body 读取请求数 article := &Article{} if err := xml.Unmarshal(b, &article); err == nil { c.JSON(http.StatusOK, article) } else { c.JSON(http.StatusBadRequest, err.Error()) } }) ``` ## 默认值 对于参数的处理,经常会出现参数不存在的情况,对于是否提供默认值,gin也考虑了,并且给出了一个优雅的方案,使用c.DefaultQuery方法读取参数,其中当参数不存在的时候,提供一个默认值。使用Query方法读取正常参数,当参数不存在的时候,返回空字串。 ```go func main() { router := gin.Default() router.GET("/welcome", func(c *gin.Context) { name := c.DefaultQuery("name", "Guest") //可设置默认值 c.String(http.StatusOK, fmt.Sprintf("Hello %s ", name)) }) router.Run(":8080") } ```
顶部
收展
底部
[TOC]
目录
Gin框架 安装
Gin框架 基本路由
Gin框架 路由传值与或参数
Gin框架 路由分组和抽离
Gin框架 自定义控制器
Gin框架 中间件
Gin框架 模板
相关推荐
Go标准库