go路由
httprouter庫一個功能
gin的路由就是來自於httprouter庫,因此httprouter具有的功能gin也具有。
編碼實戰:
package main import ( "github.com/gin-gonic/gin" "net/http" ) func main(){ router := gin.Default() router.GET("/user/:name/*age",func(context *gin.Context){ name := context.Param("name") age := context.Param("age") message := name + " is " +age context.String(http.StatusOK,message) }) router.Run() }
注意點:gin框架中除了使用:來代替變量,也可以使用*來代替,不過使用*代替表示爲任意值。
http//localhost/user?fistname="zhang"&lastname="san" http//localhost/user c.DefaultQuery("firstname","zhang") c.Query("lastname")
編程:
package main import ( "github.com/gin-gonic/gin" "net/http" ) func main(){ router := gin.Default() router.GET("/user",func(context *gin.Context){ firstname := context.Default("firstname","zhang") lastname := context.Query("lastname") message := firstname + " and " +lastname context.String(http.StatusOK,message) }) router.Run() }
注意點:
1)在瀏覽器裡輸入地址,不帶任何參數,使用默認值zhang
http://localhost:8080/user
2)如果在瀏覽器裡把firstname替換,顯示替換值
li an si
3)當參數存在並且是空字符串時,不會使用默認值
http://localhost:8080/user
"" and "si"
gin多路由
http://localhost:8080/v1/login
http://localhost:8080/v1/submit
http://localhost:8080/v1/read
func 方法1(){ } v1 := router.Group("v1") { v1.Get("/login",方法1) }
代碼實戰:
package main import ( "github.com/gin-gonic/gin" "fmt" ) func loginEndpoint(c *gin.Context){ fmt.Println("this is a login method") } func submitEndpoint(c *gin.Context){ fmt.Println("this is a submit method") } func readEndpoint(c *gin.Context){ fmt.Println("this is a read method") } func main (){ router := gin.Default() v1 := router.Group("v1") { v1.GET("/login",localhostEndpoint) v1.GET("/submit",submitEndpoint) v1.GET("/read",readEndpoint) } v2 := router.Group("v2") { v2.GET("/login",localhostEndpoint) v2.GET("/submit",submitEndpoint) v2.GET("/read",readEndpoint) } router.Run() }
普通路由
r.GET("/index", func(c *gin.Context){...}) r.GET("/login", func(c *gin.Context){...}) r.Get("/login", func(c *gin.Context){...})
此外,還有一個可以匹配所有請求的方法的Any方法如下:
r.Any("/test", func(c *gin.Context){...})
爲沒有配置處理函數的路由添加處理程序,默認情況下它返回404代碼。
下面的代碼爲沒有匹配到路由的請求都返回views/404.html頁面:
r.NoRoute(func(c *gin.Context){ c.HTML(http.StatusNotFound, "views/404.html", nil) })
路由組
可以將擁有共同URL前綴的路由劃分爲一個路由組。習慣性一對{}包裹同組的路由,這只是爲了看著清晰,用不用{}包裹,功能上沒什麽區別。
func main(){ r := gin.Default() userGroup := r.Group("/user") { userGroup.GET("/index",func(c *gin.Context){...}) userGroup.GET("/login", func(c *gin.Context){...}) userGroup.POST("/login",func(c *gin.Context){...}) } shopGroup := r.Group("/shop") { shopGroup.GET("/index",func(c *gin.Context){...}) shopGroup.GET("/cart", func(c *gin.Context){...}) shopGroup.POST("/checkout", func(c *gin.Context){...}) } r.Run() }
路由組也是支持嵌套的,例如:
shopGroup := r.Group("/shop") { shopGroup.GET("/index", func(c *gin.Context){...}) shopGroup.GET("/cart", func(c *gin.Context){...}) shopGroup.POST("/checkout", func(c *gin.Context){...}) xx := shopGroup.Group("xx") Xx.GET("/oo", func(c *gin.Context){...}) }
通常我們將路由分組用在劃分業務邏輯或者劃分API版本。
路由原理
Gin框架中的路由使用的事httprouter這個庫。
其基本原理就是構造一個路由地址的前綴樹。
例子:// lesson17 路由與路由組
package main import ( "net/http" "github.com/gin-gonic/gin" ) func main(){ r := gin.Default() r.GET("/index", func(c *gin.Context){ c.JSON(http.StatusOK, gin.H{ "method": "GET", }) }) r.POST("/index",func(c *gin.Context){ c.JSON(http.StatusOK, gin.H{ "method": "POST", }) }) r.PUT("/index", func(c *gin.Context){ c.JSON(http.StatusOK, gin.H{ "method": "PUT", }) }) r.DELETE("/index", func(c *gin.Context){ c.JSON(http.StatusOK, gin.H{ "method": "DELETE", }) }) r.Any("/user", func(c *gin.Context){ Switch c.Request.Method { case "GET:" c.JSON(http.StatusOK, gin.H{"method": "GET"}) case http.MethodPost: c.JSON(http.StatusOK, gin.H{"method": "POST"}) } }) r.NoRoute(func(c *gin.Context){ c.JSON(http.StatusNotFound, gin.H{"msg": "https://wkwkk.com"}) }) dideoGroup := r.Group("/video") { videoGroup.GET("/index". func(c *gin.Context){ c.JSON(http.StatusOK, gin.H{"msg": "/video/index"}) }) videoGroup.GET("/xx", func(c *gin.Context){ c.JSON(http.StatusOK, gin.H{"msg":"/video/xx"}) }) videoGroup.GET("/oo", func(c *gin.Context){ c.JSON(http.StatusOK, gin.H{"msg": "/video/oo"}) }) } r.GET("shop/index", func(c *gin.Context){ c.JSON(http.StatusOK, gin.H{"msg": "/shop/index"}) }) r.GET("/shop/xx", func(c *gin.Context){ c.JSON(http.StatusOK, gin.H{"msg": "/shop/xx"}) }) r.GET("/shop/oo",func(c *gin.Context){ c.JSON(http.StatusOK, gin.H{"msg": "/shop/oo"}) }) r.Run(":9090") }