gpt4 book ai didi

arrays - 如何将 []string 转换为 ...string

转载 作者:IT王子 更新时间:2023-10-29 02:27:34 25 4
gpt4 key购买 nike

我想要一个函数来扫描文件夹并找到所有模板文件。然后调用 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...)

Demo on play.golang.org

关于arrays - 如何将 []string 转换为 ...string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42452415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com