type Cookie struct{
Name string
Value string
Path string// optional
Domain string// optional
Expires time.Time // optional
RawExpires string// for reading cookies only// 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}
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><title></title></head><body><a href="setCookie">产生Cookie</a><a href="getCookie">获取Cookie</a><br/>{{.}}</body></html>
服务器提供创建Cookie和获取Cookie的代码
package main
import("net/http""html/template")funcwelcome(w http.ResponseWriter, r *http.Request){
t,_:= template.ParseFiles("view/index.html")
t.Execute(w,nil)}funcsetCookie(w http.ResponseWriter, r *http.Request){
c := http.Cookie{Name:"mykey", Value:"myvalue"}
http.SetCookie(w,&c)
t,_:= template.ParseFiles("view/index.html")
t.Execute(w,nil)}funcgetCookie(w http.ResponseWriter, r *http.Request){//根据key取出Cookie//c1,_:=r.Cookie("mykey")//取出全部Cookie内容
cs := r.Cookies()
t,_:= template.ParseFiles("view/index.html")
t.Execute(w, cs)}funcmain(){
server := http.Server{Addr:":8090"}
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
http.HandleFunc("/", welcome)
http.HandleFunc("/setCookie", setCookie)
http.HandleFunc("/getCookie", getCookie)
server.ListenAndServe()}