Golang : Drop cookie to visitor's browser and http.SetCookie() example
There are times developers need to drop cookie to website visitors' browser to determine if a particular visitor has been to the website previously. Cookie can be used to customize how your website behave to a first time visitor or repeat visitor. One such example is the login page usage. Cookie will help your website to determine if the visitor is a first time visitor or not.
For this tutorial, we will learn how to use http.SetCookie()
function to drop cookie to website visitor.
Here is an example code how to change the website message depending on the timestamp inside the visitor's cookie.
package main
import (
"net/http"
"strconv"
"time"
)
func home(w http.ResponseWriter, r *http.Request) {
// setup cookie for deployment
// see http://golang.org/pkg/net/http/#Request.Cookie
// we will try to drop the cookie, if there's error
// this means that the same cookie has been dropped
// previously and display different message
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),
}
// http://golang.org/pkg/net/http/#SetCookie
// add Set-Cookie header
http.SetCookie(w, cookieMonster)
if err != nil {
w.Write([]byte("Welcome! first time visitor!"))
} else {
lasttime, _ := strconv.ParseInt(c.Value, 10, 0)
html := "Hey! Hello again!, your last visit was at "
html = html + time.Unix(lasttime, 0).Format("15:04:05")
w.Write([]byte(html))
}
}
func main() {
http.HandleFunc("/", home)
http.ListenAndServe(":8080", nil)
}
run the code above and point your browser to http://localhost:8080
and you should see this message for the first time :
Welcome! first time visitor!
refresh the page again, and this time you will see this message :
Hey! Hello again!, your last visit was at 15:16:02
If you are using Google Chrome browser, go to Views->Developer->Developer Tools and under the Resources tab, you should see Cookies->Localhost.
Delete the cookie named timevisited
and refresh the page again. This time you should be able to see the original message again :
Welcome! first time visitor!
Hope this simple tutorial can be useful to you and happy coding!
References :
See also : Golang : Storing cookies in http.CookieJar example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+32.6k Golang : How to check if a date is within certain range?
+5.3k Golang : If else example and common mistake
+21.5k Golang : Setting up/configure AWS credentials with official aws-sdk-go
+18.7k Golang : When to use public and private identifier(variable) and how to make the identifier public or private?
+12.9k Golang : Convert(cast) int to int64
+11.3k Golang : Change date format to yyyy-mm-dd
+14.3k Golang : Rename directory
+16.5k Golang : Gzip file example
+22.3k Golang : Set and Get HTTP request headers example
+11.8k Linux : How to install driver for 600Mbps Dual Band Wifi USB Adapter
+15k Golang : Get timezone offset from date or timestamp
+6k Golang : How to get capacity of a slice or array?