在Golang中设置Cookie(net / http)

编程入门 行业动态 更新时间:2024-10-24 21:26:50
本文介绍了在Golang中设置Cookie(net / http)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我尝试使用Golang的net / http设置Cookie。我有

I'm trying to set cookies with Golang's net/http. I have

package main import "io" import "net/http" import "time" func indexHandler(w http.ResponseWriter, req *http.Request) { expire := time.Now().AddDate(0, 0, 1) cookie := http.Cookie{"test", "tcookie", "/", "www.sliceone", expire, expire.Format(time.UnixDate), 86400, true, true, "test=tcookie", []string{"test=tcookie"}} req.AddCookie(&cookie) io.WriteString(w, "Hello world!") } func main() { http.HandleFunc("/", indexHandler) http.ListenAndServe(":80", nil) }

我尝试用'cookies'搜索'Golang',但没有得到任何好的结果。

I tried googling 'Golang' with 'cookies', but didn't get any good results. If anyone can point me in the right direction it would be greatly appreciated.

感谢。

推荐答案

我不是一个Go专家,但我认为你是根据请求设置cookie,不是你。您可能想在响应上设置它。 net / http中有一个 setCookie 函数。这可能有助于: golang/pkg/net/http/#SetCookie

I am not a Go expert, but I think you are setting the cookie on the request, aren't you. You might want to set it on the response. There is a setCookie function in net/http. This might help: golang/pkg/net/http/#SetCookie

func SetCookie(w ResponseWriter, cookie *Cookie)

更多推荐

在Golang中设置Cookie(net / http)

本文发布于:2023-11-26 08:05:29,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633304.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Cookie   Golang   http   net

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!