Golang Web - HTTP Cookie
Ega Prasetya
Posted on September 9, 2020
Cookie is a small data piece of data stored on the users computer by the web browser while browsing a website. So in this chapter we will learn how to create and access cookie in Go, so let`s do it.
PRACTICE
Create file main.go and registration two route.
The variable cookieName contains a string, it later on for using as a name cookie.
- Route / work as to create new cookie
- Route /delete have a work for to delete cookie and then redirect to route / until new cookie created.
Ok, so now create function handler ActionIndex(), in this function the data is a string random to save in the cookie.
The cookie can be access from method .cookie() belong objek *http.Request. This method return two informations.
- Objec Cookie
- Error, if have
Ok, so next create function handler ActionDelete(), this handler for deleted cookie with name cookieName and redirect to route / to create a new cookie.
TESTING
Run this program and see the result
Finally! Hope your enjoy it.
Posted on September 9, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.