title: Loading a needed file, relative vs absolute paths
body: <p>We have a program people can compile on their machines. It has an HTTP interface but can also be invoked by command line.</p>
  
  <p>In order to provide some nice-looking error pages for HTTP clients, we want to provide error pages. We are using a very simple solution with go's <code>html/template</code> package.</p>
  
  <p>So in order for the program to find the templates, we currently do:</p>
  
  <pre><code>func init() {
    prefStr := "path/to/http/tmpl"
    pathPrefix,err := filepath.Abs(prefStr)
    if err != nil {
      log.Warn("Template path %s is not available!", prefStr)
    }
  
    pathPrefix + "/err.html"
  }
  </code></pre>
  
  <p>Now when debugging the app, this usually works well - we are in the package's root directory, so <code>filepath.Abs()</code> resolves correctly, like so:
  <code>$GOPATH/github.com/user/repo/path/to/http/tmpl</code> (expanding <code>$GOPATH</code> correctly)</p>
  
  <p>But when we invoke the app via executable from the command line, this doesn't work. The command line could of course be invoked from anywhere on the filesystem, for convenience for example to provide a file in the current directory as a parameter.</p>
  
  <p>In short, running <code>/some/other/path/on/fs/our-executable filename.txt</code> results in the <code>init()</code> function above breaking due to wrong concatenation of the directory: it takes <code>/some/other/path/on/fs/</code> to create the absolute path, which is wrong. Thus it crashes with
  <code>panic: open /some/other/path/on/fs/path/to/http/tmpl/err.html: no such file or directory</code></p>
  
  <p>I've searched and so far only found this: 
  <a href="https://stackoverflow.com/questions/17071286/how-can-i-open-files-using-relative-paths-in-go">How can I open files using relative paths in Go?</a></p>
  
  <p>But this exactly doesn't apply for us.
  Another solution proposes to bundle compiled go resources, but this seems rather odd as the error pages are html text.</p>
  
  <p>We've also tried
  <a href="https://stackoverflow.com/a/31464648/169252">https://stackoverflow.com/a/31464648/169252</a></p>
  
  <p>but it has the same effect.</p>
  
  <p>How can we make sure that the paths are always resolved correctly? This seems rather something which shouldn't be too difficult to be done, but we haven't managed so far.</p>
  
  <p>EDIT: This is not an exact duplicate of the question <a href="https://stackoverflow.com/questions/17071286">How can I open files using relative paths in Go?</a>. As already mentioned in my question text, I already had looked it up myself. It suggests to use <code>filepath.Abs()</code>. But as laid out in my question, for us this doesn't work, as if our executable is called from different places, <code>filepath.Abs()</code> doesn't return the same value, and thus doesn't work for us.</p>