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
+12.2k Golang : Split strings into command line arguments
+48k Golang : Convert int to byte array([]byte)
+14.3k Elastic Search : Mapping date format and sort by date
+17k Golang : How to generate QR codes?
+8.9k Golang : On lambda, anonymous, inline functions and function literals
+16.8k Golang : Gzip file example
+6.6k Golang : Convert an executable file into []byte example
+10.4k Golang : Convert file content to Hex
+43.7k Golang : Get hardware information such as disk, memory and CPU usage
+8.3k Swift : Convert (cast) Character to Integer?
+12.2k Golang : Find and draw contours with OpenCV example