2017-02-25 114 views
0

我想要一个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)) 

回答

5

您可以附加...的变量名称调用该函数允许使用的切片值时,作为可变参数参数:

template.ParseFiles(tmplPathList...) 

Demo on play.golang.org

+0

我知道有一种方法,并且你向我展示了简单的方法。 – firelyu

+1

@Flimzy这是我需要的。别再问问题了。 – firelyu

相关问题