Golang : How to fix html/template : "somefile" is undefined error?
One of the most common mistakes when using html/template
package ExecuteTemplate()
function is forgetting to parse the HTML file first before executing the template.
Without parsing the HTML file, you will get the error message
html/template : "somefile or some html code" is undefined
when trying to run your program. Trouble is that this error message is not verbose enough to point out what exactly is causing the error.
To fix the error message, simply parse the file first either with
https://golang.org/pkg/html/template/#Template.Parse
https://golang.org/pkg/html/template/#Template.ParseFiles
A good habit is always remember to count the number of HTML files that you need to execute and make sure the same number of HTML files are being parsed first.
Happy coding!
References :
https://golang.org/pkg/html/template/#Template.ExecuteTemplate
See also : Golang : How to use if, eq and print properly in html template
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+7.9k Golang : Add build version and other information in executables
+45.9k Golang : Marshal and unmarshal json.RawMessage struct example
+7.3k Golang : Convert(cast) io.Reader type to string
+5.1k Golang : Qt update UI elements with core.QCoreApplication_ProcessEvents
+19.4k Golang : Measure http.Get() execution time
+8.1k Golang : Convert word to its plural form example
+6.9k Golang : Fixing Gorilla mux http.FileServer() 404 problem
+23.2k Find and replace a character in a string in Go
+11.3k Golang : Simple file scaning and remove virus example
+7.9k Golang : Configure Apache and NGINX to access your Go service example
+17.2k Golang : Clone with pointer and modify value
+24.6k Golang : Generate MD5 checksum of a file