I've been using this function on osx:
// Shortcut to get the path to the current executable
func ExecPath() string {
var here = os.Args[0]
if !strings.HasPrefix(here, "/") {
here, _ = exec.LookPath(os.Args[0])
if !strings.HasPrefix(here, "/") {
var wd, _ = os.Getwd()
here = path.Join(wd, here)
}
}
return here
}
...but its pretty messy, and it doesn't work on windows at all, and certainly not in git-bash on windows.
Is there a way of doing this cross platform?
NB. Specifically that args[0] depends on how the binary is invoked; it is in some cases only the binary itself, eg. "app" or "app.exe"; so you can't just use that.