私は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
}
...しかし、かなり面倒で、Windowsではまったく機能せず、Windowsのgit-bashでも機能しません。
このクロスプラットフォームを行う方法はありますか?
注意。具体的には、args[0] はバイナリの呼び出し方法に依存します。場合によっては、バイナリ自体のみです。「アプリ」または「app.exe」; だからそれだけでは使えません。