golang微服務框架基礎Gin基本路由使用詳解
概述
路由是自定義url地址執(zhí)行指定的函數(shù),良好的路由定義可以對seo起到很好的效果。
1. 基本路由
gin框架封裝了http庫,提供了 GET、POST、PUT、DELETE、PATCH、HEAD、OPTIONS 這些http請求方式。
使用 router.method() 來綁定路由
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ù)
以/為分割符,每個參數(shù)以“:”為參數(shù)表示動態(tài)變量,會自動綁定到路由對應的參數(shù)上
路由規(guī)則:[:]表示可以不用匹配
比如:
http://localhost:8080/user/李四/20/北京/男 將匹配 “http://localhost:8080/user/:name/:age/:address/:sex”
上面的這個鏈接中,可以通過向上面講的
使用/user/:name/:age/:address/:sex來分別匹配李四、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))
})
注意:但是不會匹配 /user/ 或者 /user
訪問:http://localhost:8080/user/李四/20/北京/男
結果:
"[{name 李四} {age 20} {address 北京} {sex 男}]\n"
獲取URL路徑單個參數(shù)
使用gin.Context對象的Param(key)方法獲取某一個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"),
})
})
訪問:http://localhost:8080/login/15949629528/123456
結果:
{ name: "15949629528", password: "123456" }
獲取URL中指定的參數(shù)
GET、POST請求
獲取URL中路徑值和獲取參數(shù)不一樣
比如:
http://localhost:8080/login?name=張三&password=123456
可以使用接下在的方法獲取請求參數(shù)name、password的值。
//返回URL中key的值 func (c *Context) Query(key string) string
//GET請求
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請求
router.POST("/login", func(c *gin.Context) {
//{"name":"張三","password":"123456"}
c.JSON(http.StatusOK, gin.H{
"name": c.Query("name"),
"password": c.Query("password"),
})
})
訪問:http://localhost:8080/login?name=張三&password=123456
輸出內容如下:
{ name: "張三", password: "123456" }
獲取指定默認值的參數(shù)的
帶有默認值的接收 GET、POST請求
gin框架當然也想到了這么一點,gin.Context.DefaultQuery()方法,允許你指定接收的參數(shù)名,以及沒有接收到該參數(shù)值時,設置的默認值,聲明如下:
func (c *Context) DefaultQuery(key, defaultValue string) string
只有當請求沒有攜帶key,那么此時的默認值就會生效。其他情況,默認值不生效。即使URL中的該key的值為空,那么也不會啟用默認值,獲取的值就是空。
注意,這是獲取URL中的參數(shù)值
//GET請求
router.GET("/user", func(c *gin.Context) {
//{ name: "張三", password: "123456" }
c.JSON(http.StatusOK, gin.H{
"name": c.DefaultQuery("name", "默認張三"),
"password": c.DefaultQuery("password", "默認密碼"),
})
})
//POST請求
router.POST("/user", func(c *gin.Context) {
//{"name":"張三","password":"默認密碼"}
c.JSON(http.StatusOK, gin.H{
"name": c.DefaultQuery("name", "默認張三"),
"password": c.DefaultQuery("password", "默認密碼"),
})
})
訪問:http://localhost:8080/user?password=
輸出內容如下:
{ name: "默認張三", password: "默認密碼" }
以上就是golang微服務框架Gin基本路由使用詳解的詳細內容,更多關于Gin基本路由的資料請關注腳本之家其它相關文章!
相關文章
詳解Go語言Sync.Pool為何不加鎖也能夠實現(xiàn)線程安全
在這篇文章中,我們將剖析sync.Pool內部實現(xiàn)中,介紹了sync.Pool比較巧妙的內部設計思路以及其實現(xiàn)方式。在這個過程中,也間接介紹了為何不加鎖也能夠實現(xiàn)線程安全,感興趣的可以學習一下2023-04-04

