GO语言gin框架路由

字号+ 编辑: Snake 修订: SyncLWT 来源: 原创 2023-09-05 我要说两句(0)

gin的路由就是来自于httprouter库,因此httprouter具有的功能gin也具有。

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


阅完此文,您的感想如何?
  • 有用

    0

  • 没用

    0

  • 开心

    0

  • 愤怒

    0

  • 可怜

    0

1.如文章侵犯了您的版权,请发邮件通知本站,该文章将在24小时内删除;
2.本站标注原创的文章,转发时烦请注明来源;
3.Q群: 2702237 13835667

相关课文
  • GO语言GORM如何更新字段

  • gorm如何创建记录与模型定义需要注意什么

  • gorm一般查询与高级查询

  • GORM时间戳跟踪及CURD(增删改查)

我要说说
网上嘉宾点评