在Gin框架中设置和获取Cookie的方法如下
package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
func main() {
r := gin.Default()
r.GET("/cookie", func(c *gin.Context) {
cookie, err := c.Cookie("gin_cookie")
if err != nil {
cookie = "NotSet"
c.SetCookie("gin_cookie", "test", 3600, "/", "localhost", false, true)
}
fmt.Printf("Cookie value: %s
", cookie)
})
r.Run()
}
执行结果如下
作者:terry,如若转载,请注明出处:https://www.web176.com/gin/20703.html