Golang无法从我的bin目录中解析”template.ParseFiles()”(Revel)。

huangapple go评论83阅读模式
英文:

Golang Can't "template.ParseFiles()" from my bin (Revel)

问题

这是我之前的帖子 https://stackoverflow.com/questions/24750896/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"

一切正常。问题出在哪里?我尝试删除了我的bin文件夹,但没有帮助。

编辑:
我的环境变量:

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
英文:

Here is my previous post https://stackoverflow.com/questions/24750896/golang-template-parsefiles-not-a-directory-error. I have snippet:

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
}

and next error:

open /home/cnaize/gocode/bin/advorts/app/views/mailtemplates/feedback.txt: not a directory

If I hardcode to:

templates_path := "/home/cnaize/" + "feedback"

everything is ok.
Where is the problem? I've tried to remove my bin, but it didn't help.

EDITED:
My env variables:

export GOROOT="/usr/local/go"
export GOPATH=$HOME/gocode
export PATH=$PATH:$GOROOT/bin
export PATH="$PATH:$GOPATH/bin"

and my PATH variable:

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

答案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))
}
英文:

As per your fix, the right way to refer to the embedded templates/test.txt resource would be (using filepath.Dir):

root_path, err := osext.Executable()
template_path := filepath.Join(filepath.Dir(root_path), "templates", "test")
text_path := template_path + ".txt"

You would find a similar approach in cmd/go/build.go, for getting the package files:

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))
}

huangapple
  • 本文由 发表于 2014年7月15日 20:48:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/24758633.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定