我想要一个函数来扫描文件夹并找到所有模板文件。然后调用 template.ParseFiles 解析所有模板。我使用 var tmplPathList []string
记录所有模板,并将 tmplPathList
传递给 template.ParseFiles()
。
func ParseFiles(filenames ...string) (*Template, error)
使用 ... string
作为参数。编译错误 cannot use tmplPathList (type []string) as type string in argument to "html/template".ParseFiles
如何将 []string
转换为 ... string
?
var tmplPathList []string
for _, file := range tmplList {
tmplPathList = append(tmplPathList, dir + file.Name())
}
templates = template.Must(template.ParseFiles(tmplPathList))
最佳答案
您可以在调用函数时将 ...
附加到变量名,以允许将 slice 值用作可变参数参数:
template.ParseFiles(tmplPathList...)
关于arrays - 如何将 []string 转换为 ...string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42452415/