网站地图    收藏   

主页 > 后端 > Golang >

使用go gin来操作cookie的讲解

来源:自学PHP网    时间:2019-08-07 16:43 作者:小飞侠 阅读:

[导读] 使用go gin来操作cookie的讲解...

准确地说, 这个标题是有问题的, go gin只能给浏览器返回操作cookie的指令, 真正执行cookie操作的是浏览器。 但广泛地来讲, 说go gin操作cookie, 也是可以的(间接操作)

来看go gin代码:

package main
import (
  "github.com/gin-gonic/gin"
)
func main() {
  router := gin.Default();
  router.GET("/read_cookie", func(context *gin.Context) {
    val, _ := context.Cookie("name")
    context.String(200, "Cookie:%s", val) 
  })
  router.GET("/write_cookie", func(context *gin.Context) {
    context.SetCookie("name", "Shimin Li", 10, "/", "localhost", false, true)
  })
  router.GET("/clear_cookie", func(context *gin.Context) {
    context.SetCookie("name", "Shimin Li", -1, "/", "localhost", false, true)
  })
  router.Run(":8080")
}

开启服务。浏览器端执行读取、写入、清除的操作分别是:

http://localhost:8080/read_cookie

http://localhost:8080/write_cookie

http://localhost:8080/clear_cookie

自己玩了一下, OK.

不多说。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对自学php网的支持。如果你想了解更多相关内容请查看下面相关链接

自学PHP网专注网站建设学习,PHP程序学习,平面设计学习,以及操作系统学习

京ICP备14009008号-1@版权所有www.zixuephp.com

网站声明:本站所有视频,教程都由网友上传,站长收集和分享给大家学习使用,如由牵扯版权问题请联系站长邮箱904561283@qq.com

添加评论