Find the path to the executable
go, path
Solution
Use package osext.
It's providing function `Executable()` that returns an absolute path to the current program executable. It's portable between systems.
Online documentation
package main
import (
"github.com/kardianos/osext"
"fmt"
)
func main() {
filename, _ := osext.Executable()
fmt.Println(filename)
}
Problem
I compile a program with Go for various platforms and run it by calling a relative path or just by its name (if it is in the PATH variable). Is it possible to find out where the executable is? Say, my program is called "`foo`(`.exe`)". I can run `./foo`, `foo` (if it's in the PATH), `../../subdir/subdir/foo`. I have tried to use `os.Args[0]` and I guess I should check if the program name contains something different besides "foo". If yes, use `filepath.Abs`, if no, use (I can't find the function name, there is a function that looks through the PATH to check where the program is).