Find the path to the executable

后端 未结 3 689
半阙折子戏
半阙折子戏 2020-12-03 10:05

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 o

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-03 10:10

    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)
    }
    

提交回复
热议问题