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
+26k Golang : Convert(cast) string to uint8 type and back to string
+19.4k Golang : Archive directory with tar and gzip
+6.9k Ubuntu : connect() to unix:/var/run/php5-fpm.sock failed (13: Permission denied) while connecting to upstream
+11.6k Golang : Find and draw contours with OpenCV example
+27.1k Golang : Convert integer to binary, octal, hexadecimal and back to integer
+35.8k Golang : How to split or chunking a file to smaller pieces?
+7.5k Golang : get the current working directory of a running program
+17.7k Golang : Convert IPv4 address to decimal number(base 10) or integer
+8.7k Golang : Populate or initialize struct with values example
+15.4k Golang : Get digits from integer before and after given position example
+13.1k Golang : error parsing regexp: invalid or unsupported Perl syntax