Browse Source

Initial commit

master
jimi 9 years ago
commit
2d4ea53126
  1. 9
      README.md
  2. 35
      main.go

9
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.

35
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)
}
Loading…
Cancel
Save