Go 子文件夹中的深度匹配模板文件

Go 子文件夹中的深度匹配模板文件,go,Go,我有这样的结构: │ index.html └───partials footer.html header.html /src/main.go: 主程序包 进口( “日志” “net/http” “github.com/djivious/lanti mvc gtpl/src/controllers” ) func main(){ http.HandleFunc(“/”,controllers.Index) err:=http.ListenAndServe(“

我有这样的结构:

│   index.html
└───partials
        footer.html
        header.html
/src/main.go

主程序包
进口(
“日志”
“net/http”
“github.com/djivious/lanti mvc gtpl/src/controllers”
)
func main(){
http.HandleFunc(“/”,controllers.Index)
err:=http.ListenAndServe(“:8080”,nil)
如果错误!=零{
log.Fatal(“ListendServe:,错误)
}
}
/src/controllers/index.go

包控制器
进口(
“html/模板”
“日志”
“net/http”
)
//索引:frontpage处理程序
func索引(w http.ResponseWriter,r*http.Request){
t:=模板。新建(“索引”)
render,err:=t.ParseGlob(“./views/*.html”)
如果错误!=零{
致命(“解析:,错误)
返回
}
render.Execute(w,map[string]string{“Title”:“我的标题”,“Body”:“这是Body”})
}
我有以下模板文件的结构:

│   index.html
└───partials
        footer.html
        header.html
如何对子文件夹中的分区进行深度匹配<据我所知,code>ParseGlob不支持
**
运算符。有没有办法用标准库模板实现这一点

更新:

我尝试使用
github.com/mattn/go-zglob
(github发行版下推荐)递归列出
视图
文件夹中的所有模板文件:

matches, err := zglob.Glob(`./views/**/*.html`)
if err != nil {
    log.Fatal("Parse: ", err)
    return
}
fmt.Println(matches)
// RETURNS: [views/index.html views/partials/footer.html views/partials/header.html]
它返回一个字符串数组,但是
ParseFiles
函数需要
string
输入,并用
逗号分隔,因此,以下代码引发此错误:

message: 'cannot use matches (type []string) as type string in argument to t.ParseFiles'
我找到了解决办法。如果有人有比这更好的主意,我非常欢迎

包控制器
//github.com/djvision/lanti-mvc-gtpl/src/controllers/index.go
进口(
“fmt”
“html/模板”
“日志”
“net/http”
“操作系统”
“路径/文件路径”
)
//GetAllFilePathsInDirectory:递归获取目录中的所有文件路径,包括子目录。
func GetAllFilePathsInDirectory(dirpath字符串)([]字符串,错误){
变量路径[]字符串
err:=filepath.Walk(dirpath,func(路径字符串,info os.FileInfo,err error)错误{
如果错误!=零{
返回错误
}
if!info.IsDir(){
路径=追加(路径,路径)
}
归零
})
如果错误!=零{
返回零,错误
}
返回路径,零
}
//ParseDirectory:递归解析目录中的所有文件,包括子目录。
func ParseDirectory(dirpath字符串,文件名字符串)(*template.template,错误){
路径,错误:=GetAllFilePathsInDirectory(目录路径)
如果错误!=零{
返回零,错误
}
fmt.Println(路径)//日志记录
t:=模板。新建(文件名)
返回t.parsefile(路径…)
}
//索引:是索引处理程序
func索引(w http.ResponseWriter,r*http.Request){
呈现,错误:=ParseDirectory(“./视图”,“索引”)
如果错误!=零{
致命(“解析:,错误)
返回
}
render.Execute(w,map[string]string{“Title”:“我的标题”,“Body”:“这是Body”})
}