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.
Return variables –just like normal parameters and method receivers– are scoped to the function body.
Mentioned in the spec: Declarations and Scope:
The scope of an identifier denoting a method receiver, function parameter, or result variable is the function body.
Outside the function they are not in scope, you can't refer to them.
In your case the very simple fix is that you have to call the function, and what it returns, you can use it (you can pass it along, assign it to a variable etc.):
var Templates = template.Must(template.ParseFiles(ExtractFileNames()...))
One thing to note here is the ...
called the ellipsis. It is required because ExtractFileNames()
returns a slice, but template.ParseFiles()
has a variadic parameter:
func ParseFiles(filenames ...string) (*Template, error)
So that ...
will tell the compiler that you want to pass the slice "exploded", as the value for the variadic parameter (and not for example a single element in the variadic list...). More on this in the spec: Passing arguments to ... parameters