gin基础知识

本文详细介绍了Gin框架的基础知识,包括gin.Context的Params、HTTP方法的使用、URL路由参数、查询参数、表单处理,以及Gin的中间件应用,如文件上传、URL分组和日志管理。通过示例展示了如何在Gin中操作HTTP请求,如PostForm和GetQuery等方法的用法,并提到了Postman在测试中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. gin.Context

type Context struct {
   
   
    Request *http.Request
    Writer  ResponseWriter

    Params Params

    // Keys is a key/value pair exclusively for the context of each request. 专门用于每个请求上下文的键值对
    Keys map[string]interface{
   
   }

    // Errors is a list of errors attached to all the handlers/middlewares who used this context.
    // Errors是附加到使用此上下文的所有处理程序/中间件的错误列表。
    Errors errorMsgs

    // Accepted defines a list of manually accepted formats for content negotiation.
    // Accepted定义用于内容协商的手动接受格式列表。
    Accepted []string
    // contains filtered or unexported fields
}

Params

type Params []Param

type Param struct {
   
   
    Key   string
    Value string
}

Params is a Param-slice, as returned by the router. The slice is
ordered, the first URL parameter is also the first slice value. It is
therefore safe to read values by the index.

Params是键值对(Param)的数组切片,与Keys的键值对不同的是,它是由路由返回(决定)的。
它是有序的,URL中第一个参数存放在参数中的第一个位置。也就是说可以按照参数位置索引。
获取URL参数值
有两种方法回去参数值,

  1. ctx.Params.ByName(name string) 这个可以简写为ctx.Param(name)
  2. ctx.Params.Get(name string),注意这个不能简写为ctx.Get(name).ctx.Get(string)获取的是ctx.Keys里面的键值对的值。
    两个方法差不多,都是在Params的数组切片中找到第一个key与name匹配的键值对的value。如果没找到话就返回一个空字符串。差异是Get多返回一个bool参数,表示是否成功找到。

2. 使用GET, POST, PUT, PATCH, DELETE and OPTIONS

    // Creates a gin router with default middleware:
    // logger and recovery (crash-free) middleware
    router := gin.Default()

    router.GET("/someGet", getting)
    router.POST("/somePost", posting)
    router.PUT("/somePut", putting)
    router.DELETE("/someDelete", deleting)
    router.PATCH("/somePatch", patching)
    router.HEAD("/someHead", head)
    router.OPTIONS("/someOptions", options)

    // By default it serves on :8080 unless a
    // PORT environment variable was defined.
    router.Run()
    // router.Run(":3000") for a hard coded port

这个就是给URL绑定各种HTTP方法的请求,总共有9种。

3. URL带路由参数(Parameters in path)

  1. 形式1 冒号+参数名/:name
  2. 形式2 星号+参数名/*name
    获取参数值:ctx.Param(参数名的字符串) 是上面的ctx.Params.ByName(name string)的简写。
{
   
       
    // This handler will match /user/john but will not match neither /user/ or /user
    // :name会自动将这一段URL视为name参数,而且末尾有斜杆和没斜杆都可以识别
    // 没有name字段,即/user/或者/user不会被匹配
    router.GET("/user/:name", func(c *gin.Context) {
   
   
       name := c.Param("name")
       c.String(http.StatusOK, "Hello %s", name)
    })
    
    // However, this one will match /user/john/ and also /user/john/send
    // If no other routers match /user/john, it will redirect to /user/john/
    // 根据实际测试发现,使用星号而非冒号,获取参数的时候,会获取到前面的斜杆,而且,参数可以只有一个斜杆
    // 即 /user/bob/ 匹配,action为 "/" 但是实际运
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值