Let's Go系列--实战Cookie与Session
2022/9/15 23:17:13
本文主要是介绍Let's Go系列--实战Cookie与Session,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
在Gin中使用Cookie
下面代码是如何使用Cookie的样例,包括创建Cookie、获取Cookie和删除Cookie。
package main import ( "fmt" "net/http" "github.com/gin-gonic/gin" ) func main() { router := gin.Default() router.GET("/set-cookie", func(ctx *gin.Context) { ctx.SetCookie("sessionid", "fuck cookie", 3600, "/", "localhost", false, false) ctx.JSON(http.StatusOK, gin.H{ "msg": "set-cookie ok", }) }) router.GET("/get-cookie", func(ctx *gin.Context) { val, err := ctx.Cookie("sessionid") if err == nil { ctx.JSON(http.StatusOK, gin.H{ "msg": fmt.Sprintf("get cookie :sessionid=%v", val), }) } else { ctx.JSON(http.StatusOK, gin.H{ "msg": fmt.Sprintf("get cookie error:%v", err), }) } }) router.GET("/del-cookie", func(ctx *gin.Context) { ctx.SetCookie("sessionid", "", -1, "/", "localhost", false, false) ctx.JSON(http.StatusOK, gin.H{ "msg": "del-cookie ok", }) }) if err := router.Run(":8001"); err != nil { panic(err) } }
这篇关于Let's Go系列--实战Cookie与Session的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-04-01got an unexpected keyword argument
- 2024-03-30维多利亚的秘密 golang入坑系统
- 2024-03-29mongodb sort by date
- 2024-03-29go swagger
- 2024-03-25mongodb cdc
- 2024-03-25how to use go in vscode
- 2024-03-22mongooseserverselectionerror: connect econnrefused ::1:27017
- 2024-03-21pymongo insert_many
- 2024-03-18projection mongodb
- 2024-03-14clickhouse-go