I'm new to Go and trying to make some proof of concept. As such, I would like to pass my template file names to the template.ParseFiles
, like this:
var Templates = template.Must(template.ParseFiles("views/edit.html", "views/view.html", "views/main.html"))
To do it dynamically I'm trying to do:
func ExtractFileNames () (templateFileNames []string) {
files, _ := ioutil.ReadDir("./views")
for _, f := range files {
if strings.Contains(f.Name(), ".html") {
templateFileNames=append(templateFileNames, "views/" + f.Name())
}
}
return templateFileNames
}
var Templates = template.Must(template.ParseFiles(templateFileNames))
The function is working OK except that I'm getting an error:
undefined: templateFileNames
This is making think that maybe I'm not using the best approach to this problem.