我当前的目录结构如下所示:
App
- Template
- foo.go
- foo.tmpl
- Model
- bar.go
- Another
- Directory
- baz.go
文件 foo.go
使用 ParseFiles
在期间读取模板文件 init
。
import "text/template"
var qTemplate *template.Template
func init() {
qTemplate = template.Must(template.New("temp").ParseFiles("foo.tmpl"))
}
...
单元测试 foo.go
按预期工作。但是,我现在正在尝试运行单元测试 bar.go
和 baz.go
这两个都导入 foo.go
我试图打开时感到恐慌 foo.tmpl
。
/App/Model$ go test
panic: open foo.tmpl: no such file or directory
/App/Another/Directory$ go test
panic: open foo.tmpl: no such file or directory
我已经尝试将模板名称指定为相对目录(“./foo.tmpl”),完整目录(“〜/ go / src / github.com / App / Template / foo.tmpl”),App相对目录(“/App/Template/foo.tmpl”)和其他人,但似乎没有任何东西适用于这两种情况。单元测试失败了 bar.go
要么 baz.go
(或两者)。
我的模板文件应该放在哪里以及我应该如何调用 ParseFiles
这样无论我调用哪个目录,它总能找到模板文件 go test
从?