golang微服務(wù)框架基礎(chǔ)Gin基本路由使用詳解
概述
路由是自定義url地址執(zhí)行指定的函數(shù),良好的路由定義可以對(duì)seo起到很好的效果。
1. 基本路由
gin框架封裝了http庫(kù),提供了 GET、POST、PUT、DELETE、PATCH、HEAD、OPTIONS 這些http請(qǐng)求方式。
使用 router.method()
來(lái)綁定路由
func (group *RouterGroup) METHOD(relativePath string, handlers ...HandlerFunc) IRoutes
router := gin.Default() router.GET("/get", func(c *gin.Context) { c.JSON(200, gin.H{"message": "get方法"}) }) router.POST("/post", func(c *gin.Context) { c.JSON(200, gin.H{"message": "post方法"}) }) router.PUT("/put", func(c *gin.Context) { c.JSON(200, gin.H{"message": "put方法"}) }) router.DELETE("/delete", func(c *gin.Context) { c.JSON(200, gin.H{"message": "delete"}) }) router.PATCH("/patch", func(c *gin.Context) { c.JSON(200, gin.H{"message": "patch"}) }) router.HEAD("/head", func(c *gin.Context) { c.JSON(200, gin.H{"message": "head"}) }) router.OPTIONS("/options", func(c *gin.Context) { c.JSON(200, gin.H{"message": "options"}) }) router.Run(":9999")//指定端口 localhost:9999
2. 路由參數(shù)
獲取URL路徑全部參數(shù)
以/為分割符,每個(gè)參數(shù)以“:”為參數(shù)表示動(dòng)態(tài)變量,會(huì)自動(dòng)綁定到路由對(duì)應(yīng)的參數(shù)上
路由規(guī)則:[:]表示可以不用匹配
比如:
http://localhost:8080/user/李四/20/北京/男 將匹配 “http://localhost:8080/user/:name/:age/:address/:sex”
上面的這個(gè)鏈接中,可以通過(guò)向上面講的
使用/user/:name/:age/:address/:sex來(lái)分別匹配李四、20、北京、男
c.Params("key")
//http://localhost:8080/user/李四/20/北京/男 router.GET("/user/:name/:age/:address/:sex", func(c *gin.Context) { //打印URL中所有參數(shù) //"[{name 李四} {age 20} {address 北京} {sex 男}]\n" c.JSON(http.StatusOK, fmt.Sprintln(c.Params)) })
注意:但是不會(huì)匹配 /user/ 或者 /user
訪問(wèn):http://localhost:8080/user/李四/20/北京/男
結(jié)果:
"[{name 李四} {age 20} {address 北京} {sex 男}]\n"
獲取URL路徑單個(gè)參數(shù)
使用gin.Context對(duì)象的Param(key)方法獲取某一個(gè)key的值,方法聲明如下:
//http://localhost:8080/login/15949629528/123456 router.GET("/login/:name/:password", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{ //{ name: "15949629528", password: "123456" } "name": c.Param("name"), "password": c.Param("password"), }) })
訪問(wèn):http://localhost:8080/login/15949629528/123456
結(jié)果:
{ name: "15949629528", password: "123456" }
獲取URL中指定的參數(shù)
GET、POST請(qǐng)求
獲取URL中路徑值和獲取參數(shù)不一樣
比如:
http://localhost:8080/login?name=張三&password=123456
可以使用接下在的方法獲取請(qǐng)求參數(shù)name、password的值。
//返回URL中key的值 func (c *Context) Query(key string) string
//GET請(qǐng)求 router.GET("/login", func(c *gin.Context) { //{ name: "張三", password: "123456" } c.JSON(http.StatusOK, gin.H{ "name": c.Query("name"), "password": c.Query("password"), }) }) //POST請(qǐng)求 router.POST("/login", func(c *gin.Context) { //{"name":"張三","password":"123456"} c.JSON(http.StatusOK, gin.H{ "name": c.Query("name"), "password": c.Query("password"), }) })
訪問(wèn):http://localhost:8080/login?name=張三&password=123456
輸出內(nèi)容如下:
{ name: "張三", password: "123456" }
獲取指定默認(rèn)值的參數(shù)的
帶有默認(rèn)值的接收 GET、POST請(qǐng)求
gin框架當(dāng)然也想到了這么一點(diǎn),gin.Context.DefaultQuery()
方法,允許你指定接收的參數(shù)名,以及沒(méi)有接收到該參數(shù)值時(shí),設(shè)置的默認(rèn)值,聲明如下:
func (c *Context) DefaultQuery(key, defaultValue string) string
只有當(dāng)請(qǐng)求沒(méi)有攜帶key,那么此時(shí)的默認(rèn)值就會(huì)生效。其他情況,默認(rèn)值不生效。即使URL中的該key的值為空,那么也不會(huì)啟用默認(rèn)值,獲取的值就是空。
注意,這是獲取URL中的參數(shù)值
//GET請(qǐng)求 router.GET("/user", func(c *gin.Context) { //{ name: "張三", password: "123456" } c.JSON(http.StatusOK, gin.H{ "name": c.DefaultQuery("name", "默認(rèn)張三"), "password": c.DefaultQuery("password", "默認(rèn)密碼"), }) }) //POST請(qǐng)求 router.POST("/user", func(c *gin.Context) { //{"name":"張三","password":"默認(rèn)密碼"} c.JSON(http.StatusOK, gin.H{ "name": c.DefaultQuery("name", "默認(rèn)張三"), "password": c.DefaultQuery("password", "默認(rèn)密碼"), }) })
訪問(wèn):http://localhost:8080/user?password=
輸出內(nèi)容如下:
{ name: "默認(rèn)張三", password: "默認(rèn)密碼" }
以上就是golang微服務(wù)框架Gin基本路由使用詳解的詳細(xì)內(nèi)容,更多關(guān)于Gin基本路由的資料請(qǐng)關(guān)注本站其它相關(guān)文章!
版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非www.sddonglingsh.com所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。