Error 102 | *Template.Execute

I got this error

function on value not defined. Thr error is from handling the error in the code below

func Home(w http.ResponseWriter, r *http.Request) {
    if r.Method == "GET"{
        t, err := template.ParseFiles("templates/home.html")
        handleError(err, "Could not parse home")
        t.Execute(w, nil)
    }
}

Hey @danny, with the following code I have no issues, so your error must be related to something else that you’re doing. If you’d like to post what else is happening around that call maybe I or someone else can help.

Also make sure to check your errors, especially when calling Execute for templates so you can avoid some issues :stuck_out_tongue:

 func Home(w http.ResponseWriter, r *http.Request) {
	if r.Method == "GET" {
		t, err := template.ParseFiles("templates/home.html")
		if err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
		if err := t.Execute(w, nil); err != nil {
			http.Error(w, err.Error(), http.StatusInternalServerError)
			return
		}
	}
}

func main() {
	http.HandleFunc("/", Home)
	log.Fatal(http.ListenAndServe(":9000", nil))
}

Yeah, thanks I figured out the problem. You might want to know.

I parsed the template correctly but the issue is in the html file. I passed a range pipeline to it and I ommited the dollar sign. as in

{{ .range $key , $value := .MapValues}}
{{ $key}}
{{ value}} // i ommtted the dollar sign here and it messed the whole thing up.

Thanks for your contribution

Cool, glad it’s working. I was pretty sure it wasn’t the bit of code that you posted causing the issue :stuck_out_tongue:

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.