Standard library / net/http
http.CookieJar
Concurrency-safe interface for storing received cookies and selecting cookies for a URL. A client with a Jar uses it for redirects and requests. Expected stdout: 'yes\n'.
Formal Syntax & Structure
go syntaxgo
type CookieJar interface { SetCookies(u *url.URL, cookies []*Cookie)
Cookies(u *url.URL) []*Cookie}Working Example
Example Snippetgo
package main
import ( "fmt" "net/http" "net/http/cookiejar" "net/url")
func main() { var j http.CookieJar = must(cookiejar.New(nil)) u := must(url.Parse("https://example.invalid/")) j.SetCookies(u, []*http.Cookie{{Name: "demo", Value: "yes"}}) fmt.Println(j.Cookies(u)[0].Value)}
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) }}