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
+7.1k Golang : Get YouTube playlist
+12.2k Golang : Transform comma separated string to slice example
+7k Android Studio : How to detect camera, activate and capture example
+12.8k Golang : Date and Time formatting
+9.1k Golang : Get all countries currencies code in JSON format
+10.1k Golang : Resolve domain name to IP4 and IP6 addresses.
+6.6k Golang : Fibonacci number generator examples
+9.9k Golang : Simple Jawi(Yawi) to Rumi(Latin/Romanize) converter
+17k Golang : Clone with pointer and modify value
+17k Golang : Linked list example
+9.9k Fix ERROR 1045 (28000): Access denied for user 'root'@'ip-address' (using password: YES)
+22.4k Golang : Calculate time different