我正在尝试使用Go的net / http包设置cookie。我有:
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.domain.com", 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) }
我尝试将“ Golang”与“ cookies”进行谷歌搜索,但没有得到任何好的结果。如果有人能指出我正确的方向,将不胜感激。
我不是Go专家,但是我认为您是在请求中设置Cookie,不是吗?您可能需要在响应上进行设置。setCookienet / http中有一个功能。这可能会有所帮助:http : //golang.org/pkg/net/http/#SetCookie
setCookie
func SetCookie(w ResponseWriter, cookie *Cookie)