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.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay