1、路由参数
1.1、接收路由参数 Param
当注册路由格式为: /path/:a/:b
时,:x
指的就是路由参数,可以直接通过 context.Param("x")
获取值信息。
语法:
1
| func (c *Context) Param(key string) string
|
简单示例
1 2 3 4 5 6
| r := gin.Default() r.GET("/user/:id", func(c *gin.Context) { id := c.Param("id") })
|
完整代码示例
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
| package main
import ( "github.com/gin-gonic/gin" )
func main() { engine := gin.Default() engine.GET("/test/:name", func(context *gin.Context) { name := context.Param("name") context.JSON(200, gin.H{"msg": "success", "name": name}) }) engine.GET("/test/:name/:age", func(context *gin.Context) { name := context.Param("name") age := context.Param("age") context.JSON(200, gin.H{ "msg": "success", "name": name, "phone":age, }) }) engine.GET("/test/:name/:age/:height", func(context *gin.Context) { name := context.Param("name") age := context.Param("age") height := context.Param("height") context.JSON(200, gin.H{ "msg": "success", "name": name, "phone":age, "height":height, }) }) _ = engine.Run() }
|
请求返回:
1 2 3 4 5 6
| [lepeng@centos ~] {"msg":"success","name":"张三"} [lepeng@centos ~] {"msg":"success","name":"张三","phone":"18"} [lepeng@centos ~] {"height":"170","msg":"success","name":"张三","phone":"18"}
|
2、GET
参数
2.1、接收单值 Query、DefaultQuery、GetQuery
在 Gin 框架中可以通过 Query、DefaultQuery、GetQuery
来获取 Get
参数信息,而 Query、DefaultQuery
是对 GetQuery
的二次封装。
语法:
1 2 3
| func (c *Context) Query(key string) string func (c *Context) DefaultQuery(key, defaultValue string) string func (c *Context) GetQuery(key string) (string, bool)
|
简单示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| func Handler(c *gin.Context) { name := c.Query("name")
name := c.DefaultQuery("name", "tizi365")
id, ok := c.GetQuery("id") if !ok { } }
|
完整代码示例
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
| package main
import ( "github.com/gin-gonic/gin" )
func main() { engine := gin.Default() testReceiveGetParam(engine) _ = engine.Run() }
func testReceiveGetParam( engine *gin.Engine) { engine.GET("/receive", func(context *gin.Context) { name := context.Query("name") age := context.DefaultQuery("age","18") home, ok := context.GetQuery("home") context.PureJSON(200,gin.H{ "msg":"success", "context.Query->name":name, "context.DefaultQuery->age":age, "context.GetQuery->home":home, "context.GetQuery->ok":ok, }) }) }
|
请求返回:
1 2 3 4 5 6 7
| [lepeng@centos ~] {"context.DefaultQuery->age":"18","context.GetQuery->ok":false,"context.GetQuery->home":"","context.Query->name":"","msg":"success"}
[lepeng@centos ~] {"context.DefaultQuery->age":"23","context.GetQuery->ok":true,"context.GetQuery->home":"北京","context.Query->name":"小明","msg":"success"}
|
2.2、接收数组 QueryArray("param[]")
、GetQueryArray("param[]")
在 Gin 框架中可以通过 QueryArray("param[]")
和 GetQueryArray("param[]")
获取GET
方式提交中的数组值信息,而QueryArray
是对GetQueryArray
二次封装, 具体使用参考下面代码:
a.代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package main import ( "github.com/gin-gonic/gin" "go-use/practise" ) func main() { engine := gin.Default() TestReceiveGetArrayParam(engine) _ = engine.Run() }
func TestReceiveGetArrayParam(engine *gin.Engine) { engine.GET("/getArr", func(context *gin.Context) { nameList := context.QueryArray("name[]") context.JSON(200,gin.H{ "arr": nameList, }) }) }
|
2.3、接收 Map QueryMap("param")
、GetQueryMap("param")
在 Gin 框架中可以通过 QueryMap("param")
和GetQueryMap("param")
获取GET
方式提交中的map
值信息,而QueryMap
是对GetQueryMap
二次封装,具体使用参考下面代码:
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| package main import ( "github.com/gin-gonic/gin" "go-use/practise" )
func main() { engine := gin.Default() TestRecGetMapParam(engine) _ = engine.Run() }
func TestRecGetMapParam(engine *gin.Engine) { engine.GET("/getMap", func(context *gin.Context) { queryMap := context.QueryMap("score") context.JSON(200,gin.H{ "map":queryMap, }) }) }
|
3、POST
参数
3.1、接收单值 PostForm、DefaultPostForm、GetPostForm
在 Gin 框架中可以通过 PostForm、DefaultPostForm、GetPostForm
来获取 Post
提交的参数信息,而 PostForm、DefaultPostForm
同样是对 GetPostForm
的二次封装。
语法:
1 2 3
| func (c *Context) PostForm(key string) string func (c *Context) DefaultPostForm(key, defaultValue string) string func (c *Context) GetPostForm(key string) (string, bool)
|
简单示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| func Handler(c *gin.Context) { name := c.PostForm("name")
name := c.DefaultPostForm("name", "tizi365")
id, ok := c.GetPostForm("id") if !ok { } }
|
代码示例
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
| package main import ( "github.com/gin-gonic/gin" "go-use/practise" ) func main() { engine := gin.Default() practise.TestRecPostSingleValue(engine) _ = engine.Run() }
func TestRecPostSingleValue(engine *gin.Engine) { engine.POST("/postSingle", func(context *gin.Context) { name := context.PostForm("name") age := context.DefaultQuery("age", "22") home, ok := context.GetPostForm("home") context.JSON(200, gin.H{ "postForm": name, "DefaultQuery": age, "GetPostForm.home": home, "GetPostForm.ok": ok, }) }) }
|
请求返回:
1 2 3 4 5 6 7
| [lepeng@centos ~] {"DefaultQuery":"22","GetPostForm.home":"","GetPostForm.ok":false,"postForm":""}
➜ ~ curl -X POST http://127.0.0.1:8080/postSingle -d "age=40&home=南京&name=张三" {"DefaultQuery":"22","GetPostForm.home":"南京","GetPostForm.ok":true,"postForm":"张三"}
|
3.2、接收数组 PostFormArray("param[]")
、GetPostFormArray("param[]")
在Gin
框架中可以通过 PostFormArray("param[]")
和GetPostFormArray("param[]")
获取POST
方式提交中的数组值信息,而PostFormArray
是对GetPostFormArray
二次封装,具体使用参考下面代码:
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| package main import ( "github.com/gin-gonic/gin" "go-use/practise" ) func main() { engine := gin.Default() practise.TestRecPostArrValue(engine) _ = engine.Run() }
func TestRecPostArrValue(engine *gin.Engine) { engine.POST("/postArr", func(context *gin.Context) { arr := context.PostFormArray("name") context.JSON(200, gin.H{ "postArr": arr, }) }) }
|
请求返回:
1 2
| [lepeng@centos ~] {"postArr":["张三","李东"]}
|
3.3、接收Map PostFormMap("param")
、GetPostFormMap("param")
在 Gin 框架中可以通过 PostFormMap("param")
和GetPostFormMap("param")
获取POST
方式提交中的映射(map
)信息,具体使用参考下面代码:
代码示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package main import ( "github.com/gin-gonic/gin" "go-use/practise" ) func main() { engine := gin.Default() practise.TestRecPostMapValue(engine) _ = engine.Run() }
func TestRecPostMapValue(engine *gin.Engine) { engine.POST("/postMap", func(context *gin.Context) { formMap := context.PostFormMap("score") context.JSON(200,gin.H{"map":formMap}) }) }
|
请求返回:
1 2
| [lepeng@centos ~] {"map":{"数学":"100","语文":"100"}}
|
3.4、接收 JSON BindJSON(¶m)
在 Gin
框架中可以通过 BindJSON(¶m)
来接收提交的json
格式数据,具体使用参考下面代码:
1.直接赋值
a.代码示例:
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
| package main import ( "github.com/gin-gonic/gin" "go-use/practise" ) func main() { engine := gin.Default() practise.TestRecPostJson(engine) _ = engine.Run() }
func TestRecPostJson(engine *gin.Engine) { engine.POST("/postJson", func(context *gin.Context) { param := make(map[string]interface{}) err := context.BindJSON(¶m) if err != nil { context.JSON(500, gin.H{"err": err}) return } context.JSON(200, gin.H{"return": param}) }) }
|
请求返回:
1 2 3 4 5 6 7 8
| [lepeng@centos ~] "name":"张三", "age":20, "likes":["打游戏","旅游"] }' # 返回 {"return":{"age":20,"likes":["打游戏","旅游"],"name":"张三"}}
|
2.绑定到结构体
代码示例:
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
| package main import ( "github.com/gin-gonic/gin" "go-use/practise" ) func main() { engine := gin.Default() practise.TestRecPostJson2(engine) _ = engine.Run() }
type People struct { Name string `json:"name"` Age int `json:"age"` Likes []string `json:"likes"` }
func TestRecPostJson2(engine *gin.Engine) { engine.POST("/postJson2", func(context *gin.Context) { people := &People{} err := context.BindJSON(&people) if err != nil { context.JSON(500, gin.H{"err": err}) } context.JSON(200, gin.H{"return": people}) }) }
|
请求和返回同上。