2017-02-25 46 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

相關問題