Go语言 Gin处理请求参数

Posted jeikerxiao

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Go语言 Gin处理请求参数相关的知识,希望对你有一定的参考价值。

Go语言 Gin处理请求参数

1.获取Get 请求参数

获取Get请求参数的常用3个函数:

  • func (c *Context) Query(key string) string
  • func (c *Context) DefaultQuery(key, defaultValue string) string
  • func (c *Context) GetQuery(key string) (string, bool)

2.获取Post请求参数

获取Post请求参数的常用3个函数,与Get获取参数类似:

  • func (c *Context) PostForm(key string) string
  • func (c *Context) DefaultPostForm(key, defaultValue string) string
  • func (c *Context) GetPostForm(key string) (string, bool)

3.获取URL路径参数

获取URL路径参数,指的是获取 /user/:id 这类型路由绑定的参数,这个例子绑定了一个参数id。

获取url路径参数常用函数:

  • func (c *Context) Param(key string) string

4.将请求参数绑定到struct对象

前面获取参数的方式都是一个个参数的读取,实际应用中,Gin框架支持将请求参数自动绑定到一个struct对象,这种方式支持Get/Post请求,也支持http请求body内容为json/xml格式的参数。

示例

package main

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

/*
gin 获取请求参数
*/
func main() 
	// 1.初始化实例
	r := gin.Default()

	// 2.路由。localhost:8080/get?name=xiao
	r.GET("/get", func(c *gin.Context) 
		// 直接通过Query获取
		name := c.Query("name")
		c.JSON(http.StatusOK, gin.H
			"parms_name": name,
		)
	)
	// 1)正常取值:localhost:8080/title?title=xiao
	// 2) 无值,则取设置的默认值(default_title):localhost:8080/title
	r.GET("/title", func(c *gin.Context) 
		// 带默认值的参数获取
		title := c.DefaultQuery("title", "default_title")
		c.JSON(http.StatusOK, gin.H
			"title": title,
		)
	)
	// 1)返回参数判断是否有值
	// 2) 参数不存在:localhost:8080/value
	// 3) 参数存在: localhost:8080/value?value=xiao
	r.GET("/value", func(c *gin.Context) 
		value, ok := c.GetQuery("value")
		if !ok 
			// 如果参数不存在
			c.JSON(http.StatusOK, gin.H
				"message": "value not exist.",
			)
			return
		
		c.JSON(http.StatusOK, gin.H
			"value": value,
		)
	)
	// 1)post 默认,取参数值
	r.POST("/post", func(c *gin.Context) 
		// 直接获取post form参数值,为string类型
		name1 := c.PostForm("name")

		c.JSON(http.StatusOK, gin.H
			"name1": name1,
		)
	)
	// 2)post 带默认值,取参数值
	r.POST("/defaultPost", func(c *gin.Context) 
		// 设置默认值,获取参数
		name2 := c.DefaultPostForm("name", "default_name")

		c.JSON(http.StatusOK, gin.H
			"name2": name2,
		)
	)
	// 3)post 判断是否存在值,取参数值
	r.POST("/getPost", func(c *gin.Context) 
		// 判断参数是否在
		name3, ok := c.GetPostForm("name")
		if !ok 
			// 参数不存在
			c.JSON(http.StatusOK, gin.H
				"message": "name param not exist.",
			)
			return
		
		c.JSON(http.StatusOK, gin.H
			"name3": name3,
		)
	)
	// 获取请求url中的值
	r.GET("/user/:id", func(c *gin.Context) 
		// 获取请求url中的参数id
		id := c.Param("id")
		c.JSON(http.StatusOK, gin.H
			"id": id,
		)
	)
	// 绑定struct对象
	type User struct 
		Name string `json:"name" form:"name"`
		Age  int    `json:"age" form:"age"`
	
	// 绑定参数:localhost:8080/user
	/*
		
		    "name":"xi",
		    "age":18
		
	*/
	r.POST("/user", func(c *gin.Context) 
		// 初始化对象
		u := User
		// 通过 ShouldBind 函数,将请求参数绑定到 struct 对象。
		if c.ShouldBind(&u) == nil 
			// 绑定成功,输出请求参数
			c.JSON(http.StatusOK, gin.H
				"name": u.Name,
				"age":  u.Age,
			)
			return
		
		c.JSON(http.StatusOK, gin.H
			"message": "绑定失败",
		)
	)

	// 3.启动服务
	r.Run(":8080")

以上是关于Go语言 Gin处理请求参数的主要内容,如果未能解决你的问题,请参考以下文章

Go语言 Gin处理响应

Go语言 Gin处理响应

GO语言(十三):使用 Go 和 Gin 开发 RESTful API(下)

基于go-gin框架的web服务框架之websocket(二)

Go基础Http编程

go GCM加密解密 gin中间件加密解密,gin文件流处理