func handler(w http.ResponseWriter, r *http.Request) { cookies := r.Cookies() // do something with the cookies }
func handler(w http.ResponseWriter, r *http.Request) { cookie := http.Cookie{Name: "username", Value: "John"} http.SetCookie(w, &cookie) }This code defines an HTTP handler function that creates a new http.Cookie struct and sets its name and value fields. It then sets the cookie in the HTTP response using the SetCookie() method, which takes the ResponseWriter and a pointer to the cookie struct as arguments. The net/http package provides the ability to manage cookies in HTTP requests and responses. The package also provides other functionalities to manage HTTP requests and responses.