O'Reilly logo

Go Standard Library Cookbook by Radomir Sohlich

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How it works...

The net/http package also provides the functions and mechanisms to operate on cookies. The sample code presents how to set/get and remove the cookies. The SetCookie function accepts the Cookie struct pointer that represents the cookies, and naturally the ResponseWriter. The Name, Value, Domain, and expiration are set directly in the Cookie struct. Behind the scenes, the SetCookie function writes the header to set the cookies.

The cookie values could be retrieved from the Request struct. The method Cookie with the name parameter returns the pointer to the Cookie, if the cookie exists in the request.

To list all cookies within the request, the method Cookies could be called. This method returns the slice of the Cookie structs ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required