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
+13.9k Golang : Simple word wrap or line breaking example
+22.3k Golang : Convert Unix timestamp to UTC timestamp
+24.8k Golang : Create PDF file from HTML file
+11.5k Golang : convert(cast) float to string
+19.6k Golang : Count JSON objects and convert to slice/array
+10.3k Golang : Select region of interest with mouse click and crop from image
+8.5k Golang : Executing and evaluating nested loop in html template
+5.8k Golang : Compound interest over time example
+4.9k Swift : Convert (cast) Float to Int or Int32 value
+5.6k Javascript : How to replace HTML inside <div>?
+6.5k Golang : Experimental emojis or emoticons icons programming language