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
+35.6k Golang : Smarter Error Handling with strings.Contains()
+11.6k Swift : Convert (cast) Float to String
+10.1k Golang : Edge detection with Sobel method
+16.8k Golang : Get own process identifier
+17.4k Golang : Check if IP address is version 4 or 6
+14.3k Golang : Get uploaded file name or access uploaded files
+8.9k Golang : Accept any number of function arguments with three dots(...)
+7.4k Golang : Accessing dataframe-go element by row, column and name example
+12.2k Golang : calculate elapsed run time
+31.7k Golang : How to convert(cast) string to IP address?
+14k Golang : Human readable time elapsed format such as 5 days ago
+13.7k Android Studio : Password input and reveal password example