Golang net/http.Request.AddCookie() and Cookie() functions example

package net/http

Golang net/http.Request.AddCookie() and Cookie() functions usage example

 func someFunction(w http.ResponseWriter, r *http.Request) {
 ..
 c, err := r.Cookie("timevisited") 

 expire := time.Now().AddDate(0, 0, 1)

 cookieMonster := &http.Cookie{
 Name: "timevisited",
 Expires: expire,
 Value: strconv.FormatInt(time.Now().Unix(), 10),
 }

 r.AddCookie(cookieMonster)
 ...
 }

References :

https://www.socketloop.com/tutorials/golang-drop-cookie-to-visitor-s-browser-and-http-setcookie-example

http://golang.org/pkg/net/http/#Request.Cookie

http://golang.org/pkg/net/http/#Request.AddCookie

Advertisement