session和cookie
一、cookie
cookie 里面存储sessionId 浏览器端存储, 服务器如何告知浏览器存储某些数据,响应头 Set-Cookie:k=v()
浏览器再次请求中会将这些数据携带
session:浏览器关闭就失效
package main import ( "fmt" "net/http" "strconv" "time" ) func main(){ addr :=":9099" http.HandleFunc("/set/", func(writer http.ResponseWriter, request *http.Request) { cookie :=http.Cookie{ Name: "unixtime", Value: strconv.FormatInt(time.Now().Unix(),10), } http.SetCookie(writer,&cookie) }) http.HandleFunc("/get/", func(writer http.ResponseWriter, request *http.Request) { cookies :=request.Cookies() fmt.Println(cookies) unixtime,err :=request.Cookie("unixtime") fmt.Println(unixtime,err) tid,err :=request.Cookie("tid") fmt.Println(tid,err) }) http.ListenAndServe(addr,nil) }