Reputation: 8000
In Go, it's possible to prefix the final parameter in a function with the ... notation to indicate it is a variadic parameter.
template.ParseFiles is one such function:
func (t *Template) ParseFiles(filenames ...string) (*Template, error)
I am trying to create a function of my own which sets up various common features of my templates and I'd like the calling function to pass in the list of files that need to be parsed but I'm not sure how.
For example if my code looked like this:
type templateMap map[string]*template.Template
func (tmpl templateMap) AddTemplate(name string, files ...string) {
tmpl[name] = template.Must(template.ParseFiles(files)).Delims("{@","@}")
}
I get an error:
cannot use files (type []string) as type string in function argument
How do I wrapper variadic parameters?
Upvotes: 1
Views: 218
Reputation: 43939
To pass a slice in place of the variadic argument of a function, simply suffix it with ...
. So in your example code, you would instead want:
tmpl[name] = template.Must(template.ParseFiles(files...)).Delims("{@","@}")
Here is a simple example of the concept: http://play.golang.org/p/TpYNxnAM_5
Upvotes: 5