Skip to content

Instantly share code, notes, and snippets.

@mantyr
Last active April 16, 2016 15:22
Show Gist options
  • Save mantyr/afad4e33a30b70f93347694a1adb9240 to your computer and use it in GitHub Desktop.
Save mantyr/afad4e33a30b70f93347694a1adb9240 to your computer and use it in GitHub Desktop.
Get directory of binary program
package main
import (
"fmt"
"log"
"os"
"path/filepath"
)
func main() {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
fmt.Println(dir)
}
package main
// moved to github.com/mantyr/runner.GetDirBin()
func GetDirBin() (dir string, err error) {
dir, err = filepath.Abs(filepath.Dir(os.Args[0]))
if strings.Index(dir, "/tmp/go-build") == 0 || err != nil{
dir, err = os.Getwd()
}
return
}
Stack Overflow question:
Golang: How to Get the Directory of the Currently running File?
http://stackoverflow.com/questions/18537257/golang-how-to-get-the-directory-of-the-currently-running-file
Toster question:
Как в Go получить абсолютный путь запущенной программы?
https://toster.ru/q/149611
package main
import (
"github.com/kardianos/osext"
"fmt"
"log"
)
func main() {
folderPath, err := osext.ExecutableFolder()
if err != nil {
log.Fatal(err)
}
fmt.Println(folderPath)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment