Go gin框架之Cookie
- gin框架其实自带有一个"简易"的cookie体系,但是麻雀虽小,但五脏俱全嘛.使用起来也很简单,这里就做一个演示
func TestLab(ctx *gin.Context) {
rand.Seed(time.Now().UnixNano())
userid := strconv.Itoa(rand.Intn(89999) + 10000)
ctx.SetCookie("userid", userid, 60*10, "/", "localhost", false, true)
ctx.JSON(200, gin.H{"message": "OK"})
}
func TestGetCookie(ctx *gin.Context) {
userid, err := ctx.Cookie("userid")
var msg string
if err == nil {
msg = fmt.Sprintf("your userid is %s", userid)
} else {
msg = "not find your userid"
}
ctx.JSON(200, gin.H{"message": msg})
}
实机演示
