本文介绍了在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)
发布评论