go post 参数_Golang GinWeb框架快速入门/参数解析

简介


7da2e04a7cde61d6c7ae726dbd094ac7.png

Gin是Golang写的Web框架, 功能类似另一个Go框架Martini(暂停维护https://github.com/go-martini/martini), Gin内部使用定制版本的httprouter(一款轻量级高性能HTTP请求路由器,或叫多路复用器), 速度是Martini的40倍, Gin拥有强大的性能,高效率,以及可扩展性, 所以赶快用起来吧!

安装


Go版本要求: Go1.12及以上

1. 执行以下命令安装最新版本Gin

$ go get -u github.com/gin-gonic/gin

2. 在你的代码中导入

import "github.com/gin-gonic/gin"

3. (可选)导入net/http包, 如果你要使用其中的常量,比如http.StatusOK,则需要导入

import "net/http"

快速开始


编写main.go,写入以下代码并执行go run main.go, 访问http://localhost:8080/ping, 就可以得到响应消息{"message": "pong"}

package mainimport "github.com/gin-gonic/gin"func main() {  r := gin.Default()  //创建默认Gin引擎Engine,内部默认开启了日志和异常恢复中间件  r.GET("/ping", func(c *gin.Context) {    c.JSON(200, gin.H{      "message": "pong",    })  })  r.Run() //默认在localhost:8080监听}

基准测试

1043fec278a7a7884ca8cad2d01b8096.png

...

测试结果说明:

Benchmark name: 基准测试项

第(1)列:在固定时间内完成的重复次数, 值越大性能好

第(2)列:执行单次重复任务消耗的纳秒数, 单位ns/op, 值越低越好

第(3)列:执行单次重复任务消耗的堆内存字节数, 单位B/op, 值越低越好

第(4)列:每个重复任务平均分配内存的次数, 单位allocs/op, 值越低越好

Gin V1稳定版特性


  • 零内存分配的路由器

  • 仍然是最快的http路由器和框架

  • 完整的单元测试

  • 严格测试

  • API版本冻结,新发布的版本对你原来的代码兼容

使用jsoniter编译


jsoniter(https://github.com/json-iterator/go)是一个高性能可以替代Golang标准库encoding/json并且完全兼容的包

Gin默认使用encoding/json包,但是你可以使用以下tags修改为jsoniter重新编译源码

go build -tags=jsoniter .

API示例


你可以访问源码,查看更多接口示例代码:https://github.com/gin-gonic/examples

使用GET,POST,PUT,PATCH,DELETE,OPTIONS

func main() {  // Creates a gin router with default middleware:  // logger and recovery (crash-free) middleware  router := gin.Default()  router.GET("/someGet", getting)  router.POST("/somePost", posting)  router.PUT("/somePut", putting)  router.DELETE("/someDelete", deleting)  router.PATCH("/somePatch", patching)  router.HEAD("/someHead", head)  router.OPTIONS("/someOptions", options)  // By default it serves on :8080 unless a  // PORT environment variable was defined.  router.Run()  // router.Run(":3000") for a hard coded port 指定端口}

路径参数

func main() {  router := gin.Default()  // This handler will match /user/john but will not match /user/ or /user  //以下路由只会匹配/user/用户名, 不会匹配/user/或者/user  router.GET("/user/:name", func(c *gin.Context) {    name := c.Param("name") //使用Param方法从路径中获取参数    c.String(http.StatusOK, "Hello %s", name)  })  // However, this one will match /user/john/ and also /user/john/send  // If no other routers match /user/john, it will redirect to /user/john/  // 以下带冒号:和带星号*组成的路由可以匹配/user/用户名/或/user/用户名/动作,如果/user/用户名没有匹配到其他路由,它会自动重定向到/user/用户名/进行匹配  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)  })  // For each matched request Context will hold the route definition  // 请求上下文request Context会保存所有匹配上的路由定义到c.FullPath()方法  router.POST("/user/:name/*action", func(c *gin.Context) {    c.FullPath() == "/user/:name/*action" // true  })  router.Run(":8080")}

查询字符串参数

func main() {  router := gin.Default()  // Query string parameters are parsed using the existing underlying request object.  // The request responds to a url matching:  /welcome?firstname=Jane&lastname=Doe  // 发送测试请求:/welcome?firstname=Jane&lastname=Doe  router.GET("/welcome", func(c *gin.Context) {    firstname := c.DefaultQuery("firstname", "Guest") //如果没有获取到该键值,则使用第二个参数作为默认值    lastname := c.Query("lastname")     //上一行的完整写法:c.Request.URL.Query().Get("lastname")    c.String(http.StatusOK, "Hello %s %s", firstname, lastname)  })  router.Run(":8080")}

URL编码的多种数据类型组成的表单请求

请求内容类型为:application/x-www-form-urlencoded

Content-Type: application/x-www-form-urlencoded

package mainimport "github.com/gin-gonic/gin"func main() {  router := gin.Default()  // 模拟提交表单:curl -XPOST http://localhost:8080/form_post -d "message=消息&nick=昵称"  router.POST("/form_post", func(c *gin.Context) {    message := c.PostForm("message")    nick := c.DefaultPostForm("nick", "anonymous")    c.JSON(200, gin.H{      "status":  "posted",      "message": message,      "nick":    nick,    })  })  router.Run(":8080")}//返回结果: {"message":"消息","nick":"昵称","status":"posted"}

查询和提交Post表单相结合

package mainimport (  "fmt"  "github.com/gin-gonic/gin")func main() {  router := gin.Default()  router.POST("/post", func(c *gin.Context) {    id := c.Query("id")    page := c.DefaultQuery("page", "0")    name := c.PostForm("name")    message := c.PostForm("message")    fmt.Printf("id: %s; page: %s; name: %s; message: %s\n", id, page, name, message)    c.JSON(200, gin.H{      "id": id,      "page": page,      "name": name,      "message": message,    })  })  router.Run(":8080")}

模拟发送请求:

curl -XPOST http://localhost:8080/post?id=1234&page=1 -d "name=manu&message=this_is_great"

返回结果:

{"id":"1234","message":"this_is_great","name":"manu","page":"1"}

以Map映射作为查询字符串或Post表单参数

func main() {  router := gin.Default()  router.POST("/post", func(c *gin.Context) {    ids := c.QueryMap("ids")  //获取查询参数中的Map    names := c.PostFormMap("names")   //获取Post表单中的Map    fmt.Printf("ids: %v; names: %v\n", ids, names)  })  router.Run(":8080")}
模拟请求:curl -XPOST http://localhost:8080/post?ids[a]=1234&ids[b]=hello -d "names[first]=thinkerou&names[second]=tianou"打印结果:ids: map[a:1234 b:hello]; names: map[first:thinkerou second:tianou]

参考文档


Gin官方仓库:https://github.com/gin-gonic/gin


END已结束

cb9036105662cb3bf34223b4fdf6ca88.png

欢迎大家留言, 订阅, 交流哦!

7ed21d3435e7106837f8ca7825163901.gif

往期回顾


Golang与亚马逊对象存储服务AmazonS3快速入门

Golang+Vue实现Websocket全双工通信入门

GolangWeb编程之控制器方法HandlerFunc与中间件Middleware

Golang连接MySQL执行查询并解析-告别结构体

Golang的一种发布订阅模式实现

Golang 并发数据冲突检测器(Data Race Detector)与并发安全

Golang"驱动"MongoDB-快速入门("快码加鞭")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值