转到:指定template.ParseFiles的模板文件名 [英] Go: Specifying template filenames for template.ParseFiles
问题描述
我的当前目录结构如下所示:
应用程序
- 模板
- foo。 go
- foo.tmpl
- Model
- bar.go
- 另一
- 目录
- baz.go
文件 foo.go
使用 ParseFiles code>在
init
中读取模板文件。
导入文本/模板
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
I'已尝试将模板名称指定为相对目录(./foo.tmpl),完整目录(〜/ go / src / github.com / App / Template / foo.tmpl),应用程序相对目录/App/Template/foo.tmpl)等等,但是对于这两种情况似乎都没有效果。单元测试对于 bar.go
或 baz.go
(或两者)都失败。
我的模板文件放在哪里以及我应该如何调用 ParseFiles
,这样它总能找到模板文件,而不管我调用哪个目录 go test
from?
有用的提示:使用 os.Getwd()
和 filepath.Join()
来查找相对文件路径的绝对路径。
示例
// File:showPath.go
package main
import (
fmt
path / filepath
os
)
func main(){
cwd,_:= os.Getwd ()
fmt.Println(filepath.Join(cwd,./template/index.gtpl))
}
首先,我建议模板
文件夹仅包含用于演示文稿的模板,不包含用于文件的模板。
接下来,为了使生活更轻松,只能运行根项目目录中的文件。这将有助于使文件的路径与嵌套在子目录内的go文件保持一致。相对文件路径从当前工作目录开始,该目录是调用程序的地方。
显示当前工作目录中更改的示例
user @ user:〜/ go / src / test $ go run showPath.go
/ home / user / go / src / test / template / index.gtpl
user @ user:〜/ go / src / test $ cd newFolder /
user @ user:〜/ go / src / test / newFolder $ go run ../showPath.go
/home/user/go/src/test/newFolder/template/index.gtpl
对于测试文件,您可以通过提供文件名来运行单个测试文件。
go test foo / foo_test.go
最后,使用基本路径和 例子: My current directory structure looks like the following: The file Unit tests for I've tried specifying the template name as a relative directory ("./foo.tmpl"), a full directory ("~/go/src/github.com/App/Template/foo.tmpl"), an App relative directory ("/App/Template/foo.tmpl"), and others but nothing seems to work for both cases. The unit tests fail for either Where should my template file be placed and how should I call Helpful tip: Use Example First off, I recommend that the Next, to make life easier, only run files from the root project directory. This will help make the path to an file consistent throughout go files nested within sub directories. Relative file paths start from where the current working directory, which is where the program was called from. Example to show the change in current working directory As for test files, you can run individual test files by supplying the file name. Lastly, use a base path and the Example:
这篇关于转到:指定template.ParseFiles的模板文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!路径/ filepath $ c $
var (
basePath =./public
templatePath = filepath.Join(basePath,template)
indexFile = filepath.Join(templatePath,index.gtpl)
)
App
- Template
- foo.go
- foo.tmpl
- Model
- bar.go
- Another
- Directory
- baz.go
foo.go
uses ParseFiles
to read in the template file during init
. import "text/template"
var qTemplate *template.Template
func init() {
qTemplate = template.Must(template.New("temp").ParseFiles("foo.tmpl"))
}
...
foo.go
work as expected. However, I am now trying to run unit tests for bar.go
and baz.go
which both import foo.go
and I get a panic on trying to open 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
bar.go
or baz.go
(or both).ParseFiles
so that it can always find the template file regardless of which directory I call go test
from? os.Getwd()
and filepath.Join()
to find the absolute path of a relative file path.// File: showPath.go
package main
import (
"fmt"
"path/filepath"
"os"
)
func main(){
cwd, _ := os.Getwd()
fmt.Println( filepath.Join( cwd, "./template/index.gtpl" ) )
}
template
folder only contain templates for presentation and not go files.user@user:~/go/src/test$ go run showPath.go
/home/user/go/src/test/template/index.gtpl
user@user:~/go/src/test$ cd newFolder/
user@user:~/go/src/test/newFolder$ go run ../showPath.go
/home/user/go/src/test/newFolder/template/index.gtpl
go test foo/foo_test.go
path/filepath
package to form file paths.var (
basePath = "./public"
templatePath = filepath.Join(basePath, "template")
indexFile = filepath.Join(templatePath, "index.gtpl")
)