Exercise in determining from whence we came.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
701 B

9 years ago
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. "os/exec"
  7. "path"
  8. "github.com/kardianos/osext"
  9. )
  10. func main() {
  11. // Find location of executable with kardianos/osext
  12. exeFolder, err := osext.ExecutableFolder()
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. //
  17. // These will require that the executable is in a directory within the $PATH
  18. // Find location of executable with Args[0] and exec.LookPath
  19. // Requires import of os, os/exec, and path
  20. argZero := os.Args[0]
  21. exePath, err := exec.LookPath(argZero)
  22. if err != nil {
  23. log.Fatal(err)
  24. }
  25. lookPath := path.Dir(exePath)
  26. // Output to comapre the results
  27. fmt.Printf("osext.ExeFolder : %s\n", exeFolder)
  28. fmt.Printf("exec.LookPath : %s\n", lookPath)
  29. }