英文:
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))
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论