func handler(w http.ResponseWriter, r *http.Request) { cookie, err := r.Cookie("user") if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } fmt.Fprintf(w, "Hello %v", cookie.Value) }
cookie := &http.Cookie{ Name: "session", Value: "123", } http.SetCookie(w, cookie)In this example, we are creating a new cookie named "session" with a value of "123". The cookie is then set in the HTTP response using the "http.SetCookie" function. These examples use functions from the "net/http" package to work with cookies in HTTP requests and responses.