Standard library / net/http

http.Request.Cookie()

Find the first cookie with a given name; missing names return ErrNoCookie. Expected stdout: 'one\n'.

Formal Syntax & Structure

go syntaxgo
func (r *Request) Cookie(name string) (*Cookie, error)

Parameters & Attributes

NameTypeDescription
namestringName of the requested cookie or field.

Return Value

(*Cookie, error)

Operation result; check errors and validity indicators when returned.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/http")
func main() {  r := must(http.NewRequest("GET", "http://example.invalid", nil))  r.Header.Set("Cookie", "demo=one; demo=two")  c := must(r.Cookie("demo"))  fmt.Println(c.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)  }}
Go Reference