From 2d4ea531263f89b0cffb2e75aaaa85a336fba49e Mon Sep 17 00:00:00 2001 From: jimi Date: Tue, 1 Mar 2016 18:45:23 -0600 Subject: [PATCH] Initial commit --- README.md | 9 +++++++++ main.go | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) create mode 100644 README.md create mode 100644 main.go diff --git a/README.md b/README.md new file mode 100644 index 0000000..2691a3a --- /dev/null +++ b/README.md @@ -0,0 +1,9 @@ +## exedir + +Examples of methods for determining the directory path where our executable resides. + +- The `kardianos/osext` package is used for reference. The other methods will + require that the executable is located within the OS search path. + +- The first alternative utilizes `exec.LookPath` to find the executable within the + OS search path. diff --git a/main.go b/main.go new file mode 100644 index 0000000..fa2695c --- /dev/null +++ b/main.go @@ -0,0 +1,35 @@ +package main + +import ( + "fmt" + "log" + "os" + "os/exec" + "path" + + "github.com/kardianos/osext" +) + +func main() { + // Find location of executable with kardianos/osext + exeFolder, err := osext.ExecutableFolder() + if err != nil { + log.Fatal(err) + } + + // + // These will require that the executable is in a directory within the $PATH + + // Find location of executable with Args[0] and exec.LookPath + // Requires import of os, os/exec, and path + argZero := os.Args[0] + exePath, err := exec.LookPath(argZero) + if err != nil { + log.Fatal(err) + } + lookPath := path.Dir(exePath) + + // Output to comapre the results + fmt.Printf("osext.ExeFolder : %s\n", exeFolder) + fmt.Printf("exec.LookPath : %s\n", lookPath) +}