Gin 使用示例(三十四):Cookie
示例代码:
func main() {
router := gin.Default()
router.GET("/cookie", func(c *gin.Context) {
// 读取 Cookie
cookie, err := c.Cookie("gin_cookie")
if err != nil {
cookie = "NotSet"
// 设置 Cookie
c.SetCookie("gin_cookie", "test", 3600, "/", "localhost", false, true)
}
fmt.Printf("Cookie value: %s \n", cookie)
})
router.Run(":8088")
}
运行上述代码,访问 /cookie
,在控制台日志输出中可以看到打印的 Cookie 信息:
No Comments