1

这是我之前的帖子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
4

1 回答 1

3

根据您的修复,引用嵌入式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))
}
于 2014-07-24T13:07:52.650 回答