Standard library / net/http/cookiejar

cookiejar.Jar.SetCookies()

Store cookies received for a URL; negative MaxAge deletes matching cookies. This isolated fixture uses a nil suffix list. Expected stdout: '0\n'.

Formal Syntax & Structure

go syntaxgo
func (j *Jar) SetCookies(u *url.URL, cookies []*http.Cookie)

Parameters & Attributes

NameTypeDescription
u*url.URLURL whose cookie scope is used.
cookies[]*http.CookieCookies received for the URL.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/http"  "net/http/cookiejar"  "net/url")
func main() {  j := must(cookiejar.New(nil))  u := must(url.Parse("https://example.invalid/"))  j.SetCookies(u, []*http.Cookie{{Name: "demo", Value: "yes"}})  j.SetCookies(u, []*http.Cookie{{Name: "demo", MaxAge: -1}})  fmt.Println(len(j.Cookies(u)))}
func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference