Golang : Read, Write(Create) and Delete Cookie example
Got an email from a reader asking for a more elaborated example of how to read, write(create) and delete a cookie in Golang. Somehow the previous tutorial on dropping a cookie to visitor's browser wasn't detailed enough.
Alright, a fair request.
Below is an example of how to read, write/create and delete a cookie within a simple webserver.
NOTE: IF you are planning to read, write/create and delete multiple cookies. See how to use net/http/cookiejar
tutorial at https://www.socketloop.com/tutorials/golang-storing-cookies-in-http-cookiejar-example
Here you go!
package main
import (
"net/http"
)
func SayHelloWorld(w http.ResponseWriter, r *http.Request) {
html := "Hello World! "
w.Write([]byte(html))
}
func ReadCookie(w http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("ithinkidroppedacookie")
if err != nil {
w.Write([]byte("error in reading cookie : " + err.Error() + "\n"))
} else {
value := c.Value
w.Write([]byte("cookie has : " + value + "\n"))
}
}
// see https://golang.org/pkg/net/http/#Cookie
// Setting MaxAge<0 means delete cookie now.
func DeleteCookie(w http.ResponseWriter, r *http.Request) {
c := http.Cookie{
Name: "ithinkidroppedacookie",
MaxAge: -1}
http.SetCookie(w, &c)
w.Write([]byte("old cookie deleted!\n"))
}
func CreateCookie(w http.ResponseWriter, r *http.Request) {
c := http.Cookie{
Name: "ithinkidroppedacookie",
Value: "thedroppedcookiehasgoldinit",
MaxAge: 3600}
http.SetCookie(w, &c)
w.Write([]byte("new cookie created!\n"))
}
func main() {
mux := http.NewServeMux()
mux.HandleFunc("/", SayHelloWorld)
mux.HandleFunc("/readcookie", ReadCookie)
mux.HandleFunc("/deletecookie", DeleteCookie)
mux.HandleFunc("/createcookie", CreateCookie)
http.ListenAndServe(":8080", mux)
}
Now, to see how the cookie is being created, read and deleted. You need to follow this sequence.
- Run the program and point your web browser to
localhost:8080
- Change the URL to
localhost:8080/createcookie
to create new cookie. - Read the cookie by changing to
localhost:8080/createcookie
. - Finally, delete the cookie by changing to
localhost:8080/deletecookie
.
If you happen to view with Chrome browser, open up the inspector to see the cookie value
Hope this helps!
References:
https://www.socketloop.com/tutorials/golang-http-server-example
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
+26.8k Golang : Find files by name - cross platform example
+8.8k Golang : Handle sub domain with Gin
+8.1k Golang : Implementing class(object-oriented programming style)
+9.8k Golang : Read file and convert content to string
+17.7k Golang : How to log each HTTP request to your web server?
+16.1k Golang : Execute terminal command to remote machine example
+22.8k Golang : Randomly pick an item from a slice/array example
+9.4k Golang : Detect number of active displays and the display's resolution
+11.1k Use systeminfo to find out installed Windows Hotfix(s) or updates
+13.2k Golang : Increment string example
+6.5k Golang : Experimental emojis or emoticons icons programming language
+22.2k Golang : How to read JPG(JPEG), GIF and PNG files ?