danny
(Daniel Osineye)
August 26, 2016, 1:07am
1
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)
}
}
radovskyb
(Benjamin Radovsky)
August 26, 2016, 1:23am
2
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
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))
}
danny
(Daniel Osineye)
August 26, 2016, 9:41pm
3
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
radovskyb
(Benjamin Radovsky)
August 27, 2016, 12:11am
4
Cool, glad it’s working. I was pretty sure it wasn’t the bit of code that you posted causing the issue
1 Like
system
(system)
Closed
November 25, 2016, 12:11am
5
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.