日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢(xún)
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問(wèn)題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷(xiāo)解決方案
GolangGinWeb框架-快速入門(mén)/參數(shù)解析

[[353617]]

十余年的突泉網(wǎng)站建設(shè)經(jīng)驗(yàn),針對(duì)設(shè)計(jì)、前端、開(kāi)發(fā)、售后、文案、推廣等六對(duì)一服務(wù),響應(yīng)快,48小時(shí)及時(shí)工作處理。營(yíng)銷(xiāo)型網(wǎng)站的優(yōu)勢(shì)是能夠根據(jù)用戶(hù)設(shè)備顯示端的尺寸不同,自動(dòng)調(diào)整突泉建站的顯示方式,使網(wǎng)站能夠適用不同顯示終端,在瀏覽器中調(diào)整網(wǎng)站的寬度,無(wú)論在任何一種瀏覽器上瀏覽網(wǎng)站,都能展現(xiàn)優(yōu)雅布局與設(shè)計(jì),從而大程度地提升瀏覽體驗(yàn)。創(chuàng)新互聯(lián)公司從事“突泉網(wǎng)站設(shè)計(jì)”,“突泉網(wǎng)站推廣”以來(lái),每個(gè)客戶(hù)項(xiàng)目都認(rèn)真落實(shí)執(zhí)行。

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

安裝

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

1. 執(zhí)行以下命令安裝最新版本Gin

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

2. 在你的代碼中導(dǎo)入

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

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

 
 
 
 
  1. import "net/http" 

快速開(kāi)始

編寫(xiě)main.go,寫(xiě)入以下代碼并執(zhí)行g(shù)o run main.go, 訪問(wèn)http://localhost:8080/ping, 就可以得到響應(yīng)消息{"message": "pong"}

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

基準(zhǔn)測(cè)試

測(cè)試結(jié)果說(shuō)明:

Benchmark name: 基準(zhǔn)測(cè)試項(xiàng)

第(1)列:在固定時(shí)間內(nèi)完成的重復(fù)次數(shù), 值越大性能好

第(2)列:執(zhí)行單次重復(fù)任務(wù)消耗的納秒數(shù), 單位ns/op, 值越低越好

第(3)列:執(zhí)行單次重復(fù)任務(wù)消耗的堆內(nèi)存字節(jié)數(shù), 單位B/op, 值越低越好

第(4)列:每個(gè)重復(fù)任務(wù)平均分配內(nèi)存的次數(shù), 單位allocs/op, 值越低越好

Gin V1穩(wěn)定版特性

  • 零內(nèi)存分配的路由器
  • 仍然是最快的http路由器和框架
  • 完整的單元測(cè)試
  • 嚴(yán)格測(cè)試
  • API版本凍結(jié),新發(fā)布的版本對(duì)你原來(lái)的代碼兼容

使用jsoniter編譯

jsoniter(https://github.com/json-iterator/go)是一個(gè)高性能可以替代Golang標(biāo)準(zhǔn)庫(kù)encoding/json并且完全兼容的包

Gin默認(rèn)使用encoding/json包,但是你可以使用以下tags修改為jsoniter重新編譯源碼

 
 
 
 
  1. go build -tags=jsoniter . 

API示例

你可以訪問(wèn)源碼,查看更多接口示例代碼: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 指定端口 

路徑參數(shù)

 
 
 
 
  1. func main() { 
  2.   router := gin.Default() 
  3.  
  4.   // This handler will match /user/john but will not match /user/ or /user 
  5.   //以下路由只會(huì)匹配/user/用戶(hù)名, 不會(huì)匹配/user/或者/user 
  6.   router.GET("/user/:name", func(c *gin.Context) { 
  7.     name := c.Param("name") //使用Param方法從路徑中獲取參數(shù) 
  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.   // 以下帶冒號(hào):和帶星號(hào)*組成的路由可以匹配/user/用戶(hù)名/或/user/用戶(hù)名/動(dòng)作,如果/user/用戶(hù)名沒(méi)有匹配到其他路由,它會(huì)自動(dòng)重定向到/user/用戶(hù)名/進(jìn)行匹配 
  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.   // 請(qǐng)求上下文request Context會(huì)保存所有匹配上的路由定義到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") 

查詢(xún)字符串參數(shù)

 
 
 
 
  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.   // 發(fā)送測(cè)試請(qǐng)求:/welcome?firstname=Jane&lastname=Doe 
  7.   router.GET("/welcome", func(c *gin.Context) { 
  8.     firstname := c.DefaultQuery("firstname", "Guest") //如果沒(méi)有獲取到該鍵值,則使用第二個(gè)參數(shù)作為默認(rèn)值 
  9.     lastname := c.Query("lastname")  
  10.     //上一行的完整寫(xiě)法:c.Request.URL.Query().Get("lastname") 
  11.  
  12.     c.String(http.StatusOK, "Hello %s %s", firstname, lastname) 
  13.   }) 
  14.   router.Run(":8080") 

URL編碼的多種數(shù)據(jù)類(lèi)型組成的表單請(qǐng)求

請(qǐng)求內(nèi)容類(lèi)型為: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=昵稱(chēng)" 
  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. //返回結(jié)果: {"message":"消息","nick":"昵稱(chēng)","status":"posted"} 

查詢(xún)和提交Post表單相結(jié)合

 
 
 
 
  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") 

模擬發(fā)送請(qǐng)求:

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

返回結(jié)果:

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

以Map映射作為查詢(xún)字符串或Post表單參數(shù)

 
 
 
 
  1. func main() { 
  2.   router := gin.Default() 
  3.  
  4.   router.POST("/post", func(c *gin.Context) { 
  5.  
  6.     ids := c.QueryMap("ids")  //獲取查詢(xún)參數(shù)中的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. 模擬請(qǐng)求: 
  2. curl -XPOST http://localhost:8080/post?ids[a]=1234&ids[b]=hello -d "names[first]=thinkerou&names[second]=tianou" 
  3. 打印結(jié)果: 
  4. ids: map[a:1234 b:hello]; names: map[first:thinkerou second:tianou] 

參考文檔

Gin官方倉(cāng)庫(kù):https://github.com/gin-gonic/gin

 


新聞名稱(chēng):GolangGinWeb框架-快速入門(mén)/參數(shù)解析
本文來(lái)源:http://www.5511xx.com/article/djespsp.html