golang框架gin中间件的写法

栏目: Go · 发布时间: 7年前

package main

import (
    "fmt"
    "github.com/gin-gonic/gin"
)

func middleware() gin.HandlerFunc {
    return func(c *gin.Context) {
        fmt.Println("I am before next")
        c.Header("Access-Control-Allow-Origin", "test")
        c.Set("name", "test")
        /*
            c.Next()后就执行真实的路由函数,路由函数执行完成之后继续执行后续的代码
        */
        c.Next()
        fmt.Println("I am after next")
    }
}

func main() {
    r := gin.Default()
    r.Use(middleware())
    r.GET("/test", func(c *gin.Context) {
        fmt.Println("I am https://www.zhouchun.net")
        name, _ := c.Get("name")
        c.JSON(200, gin.H{"name": name})
    })
    r.Run("0.0.0.0:10020")
}

以上所述就是小编给大家介绍的《golang框架gin中间件的写法》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Twenty Lectures on Algorithmic Game Theory

Twenty Lectures on Algorithmic Game Theory

Tim Roughgarden / Cambridge University Press / 2016-8-31 / USD 34.99

Computer science and economics have engaged in a lively interaction over the past fifteen years, resulting in the new field of algorithmic game theory. Many problems that are central to modern compute......一起来看看 《Twenty Lectures on Algorithmic Game Theory》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

URL 编码/解码
URL 编码/解码

URL 编码/解码

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具