Can not write cookie

I’m trying to add 2 cookies to the web page, as below, but not of them is working:

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"math/rand"
	"net/http"
	"net/smtp"
	"strconv"
)

type verification struct {
	Email string
}

func verify(w http.ResponseWriter, r *http.Request) {
	decoder := json.NewDecoder(r.Body)
	var user verification
	err := decoder.Decode(&user)
	if err != nil {
		panic(err)
	}
	log.Println(user.Email)

	w.WriteHeader(http.StatusCreated)
	w.Header().Set("Content-Type", "application/json")
	resp := make(map[string]string)

	min := 1000
	max := 9999
	code := strconv.Itoa(rand.Intn(max-min) + min)
	// make sure to allow less secure apps:
	// https://myaccount.google.com/lesssecureapps
	from := "myemail@gmail.com"
	pass := "mypasscode"
	to := user.Email
	body := fmt.Sprintf("Hello, your verification code is: %v", code)

	msg := "From: " + from + "\n" +
		"To: " + to + "\n" +
		"Subject: Hello there\n\n" +
		body

	err = smtp.SendMail("smtp.gmail.com:587",
		smtp.PlainAuth("Cocoon Solutions", from, pass, "smtp.gmail.com"),
		from, []string{to}, []byte(msg))

	if err != nil {
		resp["Error"] = fmt.Sprintf("%s", err)
		jsonResp, err := json.Marshal(resp)
		if err != nil {
			log.Fatalf("Error happened in JSON marshal. Err: %s", err)
		}
		w.Write(jsonResp)
		return
	}

	log.Print("verification code sent, check your email")

	cookie := http.Cookie{Name: "email", Value: "user.Email", Path: "/"} // user.Email
	http.SetCookie(w, &cookie)

	cookie2 := http.Cookie{Name: "VerificationCode", Value: "code", Path: "/"} // code
	http.SetCookie(w, &cookie2)

	resp["VerificationCode"] = code
	jsonResp, err := json.Marshal(resp)
	if err != nil {
		log.Fatalf("Error happened in JSON marshal. Err: %s", err)
	}
	w.Write(jsonResp)
}

Knowing that in another code, it is working perfectly:

package main

import (
	"fmt"
	"html/template"
	"net/http"
)

type register struct {
	Company, Name, Mobile, Whatsapp, Country, City string
}

func registration(w http.ResponseWriter, r *http.Request) {
	fmt.Println("method:", r.Method)                                                             //get request method
	tmpl := template.Must(template.ParseFiles(fmt.Sprintf("%v", "templates/registration.html"))) // index.html
	if r.Method == "GET" {
		//	t, _ := template.ParseFiles("login.gtpl")
		tmpl.Execute(w, nil)
	} else {
		r.ParseForm()
		fmt.Println(r.Form)
		// map[city:[Dammam] company:[Aujan] mobile:[059] name:[Hasan] whatsapp:[059]]
		// logic part of log in
		fmt.Println("company:", r.Form["company"]) // Field name at the html form
		fmt.Println("name:", r.Form["name"])
		fmt.Println("mobile:", r.Form["mobile"])
		fmt.Println("whatsapp:", r.Form["whatsapp"])
		fmt.Println("country:", r.Form["country"])
		fmt.Println("city:", r.Form["city"]) // city: [Dammam]

		//map[string]string
		content := register{
			Company:  r.PostForm.Get("company"),
			Name:     r.PostForm.Get("name"),
			Mobile:   r.PostForm.Get("mobile"),
			Whatsapp: r.PostForm.Get("whatsapp"),
			Country:  r.PostForm.Get("country"),
			City:     r.PostForm.Get("city"),
		}

		cookie := http.Cookie{Name: "logged", Value: "true", Path: "/"}
		http.SetCookie(w, &cookie)

		http.Redirect(w, r, "/index", http.StatusSeeOther) // Redirect to another url
	}
}

Anythought?

The values for the “email” and “VerificationCode” cookies are not being set correctly. Instead of assigning the actual values, you are assigning the variable names as strings. Update the following lines:
cookie := http.Cookie{Name: “email”, Value: user.Email, Path: “/”}
http.SetCookie(w, &cookie)

cookie2 := http.Cookie{Name: “VerificationCode”, Value: code, Path: “/”}
http.SetCookie(w, &cookie2)

  1. To retrieve the cookies, you can use the r.Cookie function. Modify the code as follows:
    emailCookie, err := r.Cookie(“email”)
    if err == nil {
    emailValue := emailCookie.Value
    // Do something with the emailValue
    }

verificationCodeCookie, err := r.Cookie(“VerificationCode”)
if err == nil {
verificationCodeValue := verificationCodeCookie.Value
// Do something with the verificationCodeValue
}