Standard library / net/http

http.SetCookie()

Append a Set-Cookie response header for a valid cookie; call before writing headers. Expected stdout: 'demo=yes\n'.

Formal Syntax & Structure

go syntaxgo
func SetCookie(w ResponseWriter, cookie *Cookie)

Parameters & Attributes

NameTypeDescription
wResponseWriterDestination writer.
cookie*CookieInput value of the declared type; see the usage example.

Working Example

Example Snippetgo
package main
import (  "fmt"  "net/http"  "net/http/httptest")
func main() {  w := httptest.NewRecorder()  http.SetCookie(w, &http.Cookie{Name: "demo", Value: "yes"})  fmt.Println(w.Header().Get("Set-Cookie"))}
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