在Go中动态调用template.ParseFiles()而不是传入x个字符串的最佳方法是什么?

I want to load a folder of HTML templates in Go and right now I can only pass in each file path as a string in an argument.

Example:

templates = template.Must(template.ParseFiles("../web/html_templates/edit.html","../web/html_mplates/view.html"))

Works fine.

This and similar solutions won't work:

templates = template.Must(template.ParseFiles("../web/html_templates/*"))

I'd like to specify my templates in a config file but I currently can't. What's the best way to go about this?

Use ParseGlob to parse a folder of HTML templates in one API call.

templates = template.Must(template.ParseGlob("../web/html_templates/*.html"))

See the Match function documentation for the glob syntax.

You could use the fact that template.ParseFiles is a variadic function :

var templatesFiles []string
// [...]
// Here fill the slice from your config file or any other source
// [...]
templates = template.Must(template.ParseFiles(templatesFiles...))