34 lines
730 B
Go
34 lines
730 B
Go
|
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")
|
|||
|
|
|||
|
}
|