• Gin学习记录2——路由


    一. 常规路由

    package main
    
    import (
    	"net/http"
    
    	"github.com/gin-gonic/gin"
    )
    
    func index(ctx *gin.Context) {
    	ctx.String(http.StatusOK, "Hello Gin")
    }
    
    func main() {
    	r := gin.Default()
    	r.GET("/", index)
    	r.Run(":80")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    此时访问http://localhost/即可得到:
    在这里插入图片描述

    二. 动态路由

    func uid(ctx *gin.Context) {
    	userid := ctx.Param("uid")
    	ctx.String(http.StatusOK, "User ID is: "+userid)
    }
    
    r.GET("/:uid", uid)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    在这里插入图片描述

    • 缺点是不能像Django那样指定参数类型,这个

    三. 带参数的路由

    3.1 GET

    对于GET方式的参数,咱们可以使用Query系列的操作

    func (c *Context) Query(key string) (value string)
    func (c *Context) QueryArray(key string) (values []string)
    func (c *Context) QueryMap(key string) (dicts map[string]string)
    func (c *Context) DefaultQuery(key, defaultValue string) string
    
    • 1
    • 2
    • 3
    • 4

    例子:

    	r.GET("/AddInt", func(ctx *gin.Context) {
    		num := ctx.Query("num")
    		opt := ctx.DefaultQuery("option", "GET")
    		ctx.String(http.StatusOK, "num is %s option is %s", num, opt)
    	})
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    3.2 POST

    POST同理

    func (c *Context) DefaultPostForm(key, defaultValue string) string
    func (c *Context) PostForm(key string) (value string)
    func (c *Context) PostFormArray(key string) (values []string)
    func (c *Context) PostFormMap(key string) (dicts map[string]string)
    
    • 1
    • 2
    • 3
    • 4

    3.3 绑定

    通过绑定,可以将各种数据绑定到各种对象上

    func (c *Context) ShouldBind(obj any) error
    func (c *Context) ShouldBindBodyWith(obj any, bb binding.BindingBody) (err error)
    func (c *Context) ShouldBindHeader(obj any) error
    func (c *Context) ShouldBindJSON(obj any) error
    func (c *Context) ShouldBindQuery(obj any) error
    func (c *Context) ShouldBindTOML(obj any) error
    func (c *Context) ShouldBindUri(obj any) error
    func (c *Context) ShouldBindWith(obj any, b binding.Binding) error
    func (c *Context) ShouldBindXML(obj any) error
    func (c *Context) ShouldBindYAML(obj any) error
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    例如:

    type AddType struct {
    	Num1 string
    	Num2 string
    }
    
    func main() {
    	r := gin.Default()
    	r.GET("/AddInt", func(ctx *gin.Context) {
    		var num AddType
    		if err := ctx.ShouldBind(&num); err == nil {
    			ctx.String(http.StatusOK, "num1 is %s num2 is %s", num.Num1, num.Num2)
    		} else {
    			ctx.String(http.StatusBadRequest, "Error")
    		}
    	})
    	r.Run(":80")
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在这里插入图片描述
    但是空成员也不会报错:
    在这里插入图片描述

    四. 简单的路由组

    	v1 := r.Group("/v1")
    	{
    		v1.GET("/login", func(ctx *gin.Context) {
    			ctx.String(http.StatusOK, "Success")
    		})
    	}
    	v2 := r.Group("/v2")
    	{
    		v2.POST("/login", func(ctx *gin.Context) {
    			ctx.String(http.StatusOK, "Success")
    		})
    	}
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    五. 文件分组

    新建文件夹routers,并且在该文件夹下将路由分为两个:v1.gov2.go

    package routers
    
    import (
    	"net/http"
    
    	"github.com/gin-gonic/gin"
    )
    
    func V1RoutesInit(router *gin.Engine) {
    	v1Router := router.Group("/v1")
    	{
    		v1Router.GET("/user", func(c *gin.Context) {
    			c.String(http.StatusOK, "v1查询")
    		})
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    package routers
    
    import (
    	"net/http"
    
    	"github.com/gin-gonic/gin"
    )
    
    func V2RoutesInit(router *gin.Engine) {
    	v2Router := router.Group("/v2")
    	{
    		v2Router.GET("/user", func(c *gin.Context) {
    			c.String(http.StatusOK, "v2查询")
    		})
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    然后在main.go里:imort moudel/package ,比如"kanna-web/routers"
    然后即可调用:

    	routers.V1RoutesInit(r)
    	routers.V2RoutesInit(r)
    
    • 1
    • 2

    在这里插入图片描述

  • 相关阅读:
    C#编程学习
    接口测试工具的实验,Postman、Swagger、knife4j(黑马头条)
    华为手机怎样设置每月8号18号28号提醒的备忘录
    [python3] 发送微信 同步手机端
    TMD,JVM类加载原来是这样的!!!!
    【动手学深度学习-Pytorch版】门控循环单元GRU
    Python 与 Qt c++ 程序共享内存,传递图片
    python基于PHP+MyQL的科研实验室管理系统
    目标检测YOLO实战应用案例100讲-基于改进YOLO v7的智能振动分拣系统开发
    计算机竞赛 深度学习+opencv+python实现车道线检测 - 自动驾驶
  • 原文地址:https://blog.csdn.net/u011017694/article/details/132672032