成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

Golang GinWeb框架-快速入門/參數解析

開發 前端
Gin是Golang寫的Web框架, 功能類似另一個Go框架Martini(暫停維護https://github.com/go-martini/martini), Gin內部使用定制版本的httprouter(一款輕量級高性能HTTP請求路由器,或叫多路復用器), 速度是Martini的40倍, Gin擁有強大的性能,高效率,以及可擴展性, 所以趕快用起來吧!

[[353617]]

Gin是Golang寫的Web框架, 功能類似另一個Go框架Martini(暫停維護https://github.com/go-martini/martini), Gin內部使用定制版本的httprouter(一款輕量級高性能HTTP請求路由器,或叫多路復用器), 速度是Martini的40倍, Gin擁有強大的性能,高效率,以及可擴展性, 所以趕快用起來吧!

安裝

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

1. 執行以下命令安裝最新版本Gin

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

2. 在你的代碼中導入

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

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

  1. import "net/http" 

快速開始

編寫main.go,寫入以下代碼并執行go run main.go, 訪問http://localhost:8080/ping, 就可以得到響應消息{"message": "pong"}

  1. package main 
  2.  
  3. import "github.com/gin-gonic/gin" 
  4.  
  5. func main() { 
  6.   r := gin.Default()  //創建默認Gin引擎Engine,內部默認開啟了日志和異常恢復中間件 
  7.   r.GET("/ping", func(c *gin.Context) { 
  8.     c.JSON(200, gin.H{ 
  9.       "message""pong"
  10.     }) 
  11.   }) 
  12.   r.Run() //默認在localhost:8080監聽 

基準測試


測試結果說明:

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重新編譯源碼

  1. go build -tags=jsoniter . 

API示例

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

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

  1. func main() { 
  2.   // Creates a gin router with default middleware: 
  3.   // logger and recovery (crash-free) middleware 
  4.   router := gin.Default() 
  5.  
  6.   router.GET("/someGet", getting) 
  7.   router.POST("/somePost", posting) 
  8.   router.PUT("/somePut", putting) 
  9.   router.DELETE("/someDelete", deleting) 
  10.   router.PATCH("/somePatch", patching) 
  11.   router.HEAD("/someHead", head) 
  12.   router.OPTIONS("/someOptions", options) 
  13.  
  14.   // By default it serves on :8080 unless a 
  15.   // PORT environment variable was defined. 
  16.   router.Run() 
  17.   // router.Run(":3000"for a hard coded port 指定端口 

路徑參數

  1. func main() { 
  2.   router := gin.Default() 
  3.  
  4.   // This handler will match /user/john but will not match /useror /user 
  5.   //以下路由只會匹配/user/用戶名, 不會匹配/user/或者/user 
  6.   router.GET("/user/:name", func(c *gin.Context) { 
  7.     name := c.Param("name") //使用Param方法從路徑中獲取參數 
  8.     c.String(http.StatusOK, "Hello %s"name
  9.   }) 
  10.  
  11.   // However, this one will match /user/john/ and also /user/john/send 
  12.   // If no other routers match /user/john, it will redirect to /user/john/ 
  13.   // 以下帶冒號:和帶星號*組成的路由可以匹配/user/用戶名/或/user/用戶名/動作,如果/user/用戶名沒有匹配到其他路由,它會自動重定向到/user/用戶名/進行匹配 
  14.   router.GET("/user/:name/*action", func(c *gin.Context) { 
  15.     name := c.Param("name"
  16.     action := c.Param("action"
  17.     message := name + " is " + action 
  18.     c.String(http.StatusOK, message) 
  19.   }) 
  20.  
  21.   // For each matched request Context will hold the route definition 
  22.   // 請求上下文request Context會保存所有匹配上的路由定義到c.FullPath()方法 
  23.   router.POST("/user/:name/*action", func(c *gin.Context) { 
  24.     c.FullPath() == "/user/:name/*action" // true 
  25.   }) 
  26.  
  27.   router.Run(":8080"

查詢字符串參數

  1. func main() { 
  2.   router := gin.Default() 
  3.  
  4.   // Query string parameters are parsed using the existing underlying request object. 
  5.   // The request responds to a url matching:  /welcome?firstname=Jane&lastname=Doe 
  6.   // 發送測試請求:/welcome?firstname=Jane&lastname=Doe 
  7.   router.GET("/welcome", func(c *gin.Context) { 
  8.     firstname := c.DefaultQuery("firstname""Guest") //如果沒有獲取到該鍵值,則使用第二個參數作為默認值 
  9.     lastname := c.Query("lastname")  
  10.     //上一行的完整寫法:c.Request.URL.Query().Get("lastname"
  11.  
  12.     c.String(http.StatusOK, "Hello %s %s", firstname, lastname) 
  13.   }) 
  14.   router.Run(":8080"

URL編碼的多種數據類型組成的表單請求

請求內容類型為:application/x-www-form-urlencoded

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

  1. package main 
  2.  
  3. import "github.com/gin-gonic/gin" 
  4.  
  5. func main() { 
  6.   router := gin.Default() 
  7.  
  8.   // 模擬提交表單:curl -XPOST http://localhost:8080/form_post -d "message=消息&nick=昵稱" 
  9.   router.POST("/form_post", func(c *gin.Context) { 
  10.     message := c.PostForm("message"
  11.     nick := c.DefaultPostForm("nick""anonymous"
  12.  
  13.     c.JSON(200, gin.H{ 
  14.       "status":  "posted"
  15.       "message": message, 
  16.       "nick":    nick, 
  17.     }) 
  18.   }) 
  19.   router.Run(":8080"
  20. //返回結果: {"message":"消息","nick":"昵稱","status":"posted"

查詢和提交Post表單相結合

  1. package main 
  2.  
  3. import ( 
  4.   "fmt" 
  5.   "github.com/gin-gonic/gin" 
  6.  
  7. func main() { 
  8.   router := gin.Default() 
  9.  
  10.   router.POST("/post", func(c *gin.Context) { 
  11.  
  12.     id := c.Query("id"
  13.     page := c.DefaultQuery("page""0"
  14.     name := c.PostForm("name"
  15.     message := c.PostForm("message"
  16.  
  17.     fmt.Printf("id: %s; page: %s; name: %s; message: %s\n", id, page, name, message) 
  18.     c.JSON(200, gin.H{ 
  19.       "id": id, 
  20.       "page": page, 
  21.       "name"name
  22.       "message": message, 
  23.     }) 
  24.   }) 
  25.   router.Run(":8080"

模擬發送請求:

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

返回結果:

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

以Map映射作為查詢字符串或Post表單參數

  1. func main() { 
  2.   router := gin.Default() 
  3.  
  4.   router.POST("/post", func(c *gin.Context) { 
  5.  
  6.     ids := c.QueryMap("ids")  //獲取查詢參數中的Map 
  7.     names := c.PostFormMap("names")   //獲取Post表單中的Map 
  8.  
  9.     fmt.Printf("ids: %v; names: %v\n", ids, names) 
  10.   }) 
  11.   router.Run(":8080"

  1. 模擬請求: 
  2. curl -XPOST http://localhost:8080/post?ids[a]=1234&ids[b]=hello -d "names[first]=thinkerou&names[second]=tianou" 
  3. 打印結果: 
  4. ids: map[a:1234 b:hello]; names: map[first:thinkerou second:tianou] 

參考文檔

Gin官方倉庫:https://github.com/gin-gonic/gin

 

責任編輯:姜華 來源: 云原生云
相關推薦

2020-11-26 10:08:17

Golang GinW

2020-11-25 09:18:15

Golang GinW

2020-12-03 09:28:05

Golang GinW

2020-12-02 11:18:28

Golang GinW

2011-11-08 10:36:42

Java

2020-11-27 07:54:53

Golang GinW

2020-12-10 10:22:48

GinWeb中間件HTTPS

2020-11-25 09:10:39

Golang GinW

2020-12-08 12:05:48

Golang GinW框架HTTPS

2021-07-28 06:51:08

FlaskPythonWeb

2017-09-30 16:06:28

代碼注解分析

2020-12-22 08:41:21

GolangPostgreSQL數據庫

2024-03-06 09:11:34

2023-10-22 20:20:37

FiberGo

2023-07-05 08:38:48

GolangGo語言

2015-10-29 15:36:19

Redis入門

2009-01-03 14:39:00

ibmdwSpirit

2011-11-29 12:27:54

2009-09-24 15:27:41

Hibernate查詢

2010-06-24 13:35:53

GRE協議
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 色婷婷久久久久swag精品 | 日韩 欧美 二区 | 夜久久 | 青青久久久 | 亚洲女人天堂成人av在线 | 亚洲欧美自拍偷拍视频 | 久在线观看 | 久久999| 日韩在线一区二区 | 久久久国产一区二区三区 | 日韩在线播放一区 | 中文字幕电影在线观看 | 日韩精品一区二区三区在线播放 | 精品久久久久久久久久久久久久 | 精品国产99 | 爱爱视频在线观看 | 91精品一区二区三区久久久久 | 久久一级 | 亚洲免费一区二区 | 久久99久久99| 999免费网站 | 国产久 | 成人av免费播放 | 伊人久久综合 | 国产精品久久久久久久久久东京 | 精品视频在线观看 | 精品综合视频 | 国产精品久久久久久久白浊 | 免费亚洲婷婷 | 超碰人人爱 | 日本一区二区三区免费观看 | 欧美成人免费在线视频 | 国产精品18hdxxxⅹ在线 | 国产精品1区 | 国产欧美视频一区二区三区 | 久久久国产亚洲精品 | 亚洲一区高清 | 日韩精品一区二区三区高清免费 | 日韩一区在线观看视频 | 国产精品福利在线观看 | 日本激情视频网 |