DEV Community

Ega Prasetya
Ega Prasetya

Posted on

3 2

Golang Web - HTTP Cookie

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.

Alt Text

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.

Alt Text

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.

Alt Text

TESTING

Run this program and see the result

Alt Text

Finally! Hope your enjoy it.

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

AWS GenAI LIVE!

GenAI LIVE! is a dynamic live-streamed show exploring how AWS and our partners are helping organizations unlock real value with generative AI.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️