华域联盟 Golang 使用go gin来操作cookie的讲解

使用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.

不多说。

总结

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

本文由 华域联盟 原创撰写:华域联盟 » 使用go gin来操作cookie的讲解

转载请保留出处和原文链接:https://www.cnhackhy.com/110537.htm

本文来自网络,不代表华域联盟立场,转载请注明出处。

作者: sterben

发表回复

联系我们

联系我们

2551209778

在线咨询: QQ交谈

邮箱: [email protected]

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们