如何将[]字符串转换为...字符串

fir*_*lyu 0 arrays go slice

我想有一个func来扫描文件夹并找到所有模板文件.然后调用template.ParseFiles来解析所有模板.我用它var tmplPathList []string来记录所有的模板,并传递tmplPathListtemplate.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))
Run Code Online (Sandbox Code Playgroud)

tka*_*usl 5

您可以...在调用函数时附加到变量名,以允许将切片值用作varargs-parameters:

template.ParseFiles(tmplPathList...)
Run Code Online (Sandbox Code Playgroud)

在play.golang.org上演示