Follow

Follow

Handling cookies in the Go language!

Maheshwar Ligade's photo
Maheshwar Ligade
·Apr 10, 2023·

3 min read

Play this article

Table of contents

  • Setting Cookies
  • Retrieving Cookies
  • Deleting Cookies
  • Conclusion

Handling cookies in Go language is a common task in building web applications. Cookies are small pieces of data that are sent from a web server to a client browser and then stored on the client's machine. They are commonly used to maintain user sessions, remember user preferences, and track user behavior on a website.

In this article, we will explore how to handle cookies in Go language, including setting, retrieving, and deleting cookies.

Setting Cookies

Setting cookies in Go is a straightforward process. The net/http package provides a SetCookie function that allows you to set a cookie in the response header. Here is an example:

func setCookie(w http.ResponseWriter, r *http.Request) {
    c := http.Cookie{
        Name:    "username",
        Value:   "johndoe",
        Expires: time.Now().Add(24 * time.Hour),
    }
    http.SetCookie(w, &c)
}

In the example above, we create a new cookie with the name "username" and value "johndoe". We also set an expiration time of 24 hours from the current time. Finally, we call http.SetCookie to add the cookie to the response header.

Retrieving Cookies

Retrieving cookies in Go is just as easy as setting them. The net/http package provides a Cookie function that allows you to retrieve a cookie from the request header. Here is an example:

func getCookie(w http.ResponseWriter, r *http.Request) {
    c, err := r.Cookie("username")
    if err != nil {
        http.Error(w, err.Error(), http.StatusBadRequest)
        return
    }
    fmt.Fprintf(w, "Hello %s!", c.Value)
}

In the example above, we retrieve the cookie with the name "username" from the request header. If the cookie does not exist, we return a bad request error. Otherwise, we print out a greeting message that includes the value of the cookie.

Deleting Cookies

Deleting cookies in Go is also simple. To delete a cookie, you can set its expiration time to a time in the past. Here is an example:

func deleteCookie(w http.ResponseWriter, r *http.Request) {
    c := &http.Cookie{
        Name:   "username",
        Value:  "",
        MaxAge: -1,
    }
    http.SetCookie(w, c)
}

In the example above, we create a new cookie with the name "username" and set its value to an empty string. We also set its maximum age to -1, which causes it to immediately expire. Finally, we call http.SetCookie to add the cookie to the response header.

Conclusion

In this article, we have explored how to handle cookies in Go language. By using the SetCookie and Cookie functions provided by the net/http package, you can easily set, retrieve, and delete cookies in your web applications. With this knowledge, you can now build more sophisticated web applications that maintain user sessions, remember user preferences, and track user behavior on your website.

I hope this helps, you!!

More such articles:

https://medium.com/techwasti

https://www.youtube.com/channel/UCiTaHm1AYqMS4F4L9zyO7qA

https://www.techwasti.com/

\==========================**=========================

If this article adds any value to you then please clap and comment.

Let’s connect on Stackoverflow, LinkedIn, & Twitter.

Did you find this article valuable?

Support techwasti by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this