这是我之前的帖子Golang template.ParseFiles "not a directory" error。我有片段:
root_path, err := osext.Executable()
if err != nil {
return err
}
templates_path := root_path + "/app/views/mailtemplates/" + "feedback"
text_path := templates_path + ".txt"
textTmpl, err := template.ParseFiles(text_path)
if err != nil {
return err
}
和下一个错误:
open /home/cnaize/gocode/bin/advorts/app/views/mailtemplates/feedback.txt: not a directory
如果我硬编码:
templates_path := "/home/cnaize/" + "feedback"
一切都好。
问题出在哪儿?我试图删除我的垃圾箱,但它没有帮助。
编辑:
我的env variables
:
export GOROOT="/usr/local/go"
export GOPATH=$HOME/gocode
export PATH=$PATH:$GOROOT/bin
export PATH="$PATH:$GOPATH/bin"
和我的PATH
变量:
PATH=/home/cnaize/.rvm/gems/ruby-2.1.0/bin:/home/cnaize/.rvm/gems/ruby-2.1.0@global/bin:/home/cnaize/.rvm/rubies/ruby-2.1.0/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/home/cnaize/.rvm/bin:/usr/local/go/bin:/home/cnaize/gocode/bin
答案 0 :(得分:3)
根据your fix,引用嵌入式templates/test.txt
资源的正确方法是(使用filepath.Dir
):
root_path, err := osext.Executable()
template_path := filepath.Join(filepath.Dir(root_path), "templates", "test")
text_path := template_path + ".txt"
您会在cmd/go/build.go
中找到类似的方法来获取包文件:
func (gccgoToolchain) pkgpath(basedir string, p *Package) string {
end := filepath.FromSlash(p.ImportPath + ".a")
afile := filepath.Join(basedir, end)
// add "lib" to the final element
return filepath.Join(filepath.Dir(afile), "lib"+filepath.Base(afile))
}