我在undefined: msg
以tmpl.Execute
. 你应该如何在 Go 中检索 cookie?
func contact(w http.ResponseWriter, r *http.Request) {
if r.Method == "POST" {
r.ParseForm()
for k, v := range r.Form {
fmt.Println("k:", k, "v:", v)
}
http.SetCookie(w, &http.Cookie{Name: "msg", Value: "Thanks"})
http.Redirect(w, r, "/contact/", http.StatusFound)
}
if msg, err := r.Cookie("msg"); err != nil {
msg := ""
}
tmpl, _ := template.ParseFiles("templates/contact.tmpl")
tmpl.Execute(w, map[string]string{"Msg": msg})
}