Set a HTTP Cookie Response Header
·
246 words
·
2 minutes read
For many different reasons, there will be times when you need to keep data within a cookie to be sent with subsequent requests or read by the recipient. We can do this with Go’s standard library, or by using a package like gorilla’s session, but for this simple example we’ll use the standard library.
We’ve created a function called addCookie()
which takes a name, value and duration, and writes it directly against the ResponseWriter w
.
If we were to run this with go run cookie.go
and then use curl -I http:/localhost:8080
to check the response, we would see something like this: