I have a golang code as follows
package main
import (
"html/template"
"os"
"path/filepath"
)
func main() {
// Parse all templates that match the recursive glob pattern
templates := template.Must(template.ParseGlob(filepath.Join("templates", "**", "*.gohtml")))
// Execute the base template to render the combined content
err := templates.ExecuteTemplate(os.Stdout, "base.gohtml", nil)
if err != nil {
panic(err)
}
}
and the templates are in the following structure
templates
├── base.gohtml
└── nested
└── index.gohtml
when I run this program, it's showing this error
panic: html/template: "base.gohtml" is undefined
goroutine 1 [running]:
main.main()
when I move the base.gohtml into nested directory, it works as expected. is there any problem with the pattern ?
In your program filepath.Join("templates", "**", "*.gohtml")
gives you a file path that should match templates/**/*.gohtml
and since we have /**/
in there it should match any directory but not root, and then inside that directory any gohtml file which is why files in the template dir are not getting matched but everything inside nested is. And if you have any other dir, the gohtml files inside it would also get matched.
So yeah there is a problem with the pattern and more details can be found in go docs
Also more can be found out on Issue#11862 on go's repo!