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
+11.6k Golang : Setup API server or gateway with Caddy and http.ListenAndServe() function example
+26.4k Golang : How to check if a connection to database is still alive ?
+17.8k Golang : Put UTF8 text on OpenCV video capture image frame
+19.8k Golang : Reset or rewind io.Reader or io.Writer
+19.6k Golang : How to get time from unix nano example
+11.4k SSL : The certificate is not trusted because no issuer chain was provided
+11.1k Golang : Byte format example
+7.3k Golang : Gorrila set route name and get the current route name
+10k Golang : How to profile or log time spend on execution?
+14.3k Golang : Convert(cast) int to float example
+16.2k Golang : Generate QR codes for Google Authenticator App and fix "Cannot interpret QR code" error