cnaize
cnaize

Reputation: 3149

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

Here is my previous post 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

Upvotes: 1

Views: 942

Answers (1)

VonC
VonC

Reputation: 1327784

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

Upvotes: 3

Related Questions