Go 设置 cookie

简介: Go 设置 cookie

Go 设置 cookie
Go 语言中通过 net/http 包中的 SetCookie 来设置:
http.SetCookie(w ResponseWriter, cookie *Cookie)
w 表示需要写入的 response,cookie 是一个 struct,让我们来看一下 cookie 对象是怎么样

type Cookie struct {
Name string
Value string
Path string
Domain string
Expires time.Time
RawExpires string
// MaxAge=0 means no 'Max-Age' attribute specified.
// MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0'
// MaxAge>0 means Max-Age attribute present and given in seconds
MaxAge int
Secure bool
HttpOnly bool
Raw string
Unparsed []string // Raw text of unparsed attribute-value pairs
}
我们来看一个例子,如何设置 cookie
expiration := *time.LocalTime()
expiration.Year += 1
cookie := http.Cookie{Name: "username", Value: "astaxie", Expires: expiration}
http.SetCookie(w, &cookie)

目录
打赏
0
0
0
0
0
分享
相关文章
|
9月前
|
从建站到拿站 -- PHP(Cookie设置)
从建站到拿站 -- PHP(Cookie设置)
58 0
|
6月前
|
【Azure APIM】APIM 策略语句如何来设置多个Cookie值让浏览器保存
【Azure APIM】APIM 策略语句如何来设置多个Cookie值让浏览器保存
【Azure 环境】Azure应用程序网关设置set_Cookie=key=value; SameSite=Strict; HTTPOnly,AzureAD登录使用cookie时使用不了的案例记录
【Azure 环境】Azure应用程序网关设置set_Cookie=key=value; SameSite=Strict; HTTPOnly,AzureAD登录使用cookie时使用不了的案例记录
Vue项目使用Cookie,以Json格式存入与读取Cookie,设置过期时间以及删除操作
这篇文章介绍了在Vue项目中如何使用JavaScript操作Cookie,包括设置、读取、设置过期时间以及删除Cookie的方法。
366 0
|
8月前
|
蓝易云 - 如何使用CORS来允许设置Cookie
以上就是使用CORS来允许设置Cookie的方法。
46 2
什么是cookie,如何设置在浏览器页面关闭后清除cookie
什么是cookie,如何设置在浏览器页面关闭后清除cookie
620 0
百度搜索:蓝易云【php设置和获取Cookie教程。】
需要注意的是,为了确保在向浏览器发送任何输出之前设置Cookie,应该在 `<html>`标签之前或PHP脚本的顶部设置Cookie。
112 0

热门文章

最新文章