gin/cookie/main.go

34 lines
730 B
Go
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package main
import (
"fmt"
"github.com/gin-gonic/gin"
)
//cookie
//客户端请求服务端服务端发送cookie给客户端
func main() {
r := gin.Default()
//服务端要给客户端cookie
r.GET("cookie", func(c *gin.Context) {
//获取客户端是否携带cookie
cookie, err := c.Cookie("key_cookie")
if err != nil {
fmt.Println(err)
cookie = "NotSet"
}
c.SetCookie("key_cookie", "value_cookie", 60, "/", "localhost", false, true)
//设置cookie
//maxAge int 过期时间单位是秒
//path, cookie所在目录
//domain 域名
//secure 是否只能通过https访问
//httpOnly bool 是否允许别人通过js获取cookie
fmt.Println("cookie的值是:", cookie)
})
r.Run(":8000")
}