乐闻世界logo
搜索文章和话题

How to get the directory of the currently running file

3个答案

1
2
3

In Golang, you can use the Executable function from the os standard library to obtain the full path of the currently executing executable file, and then use the Dir function from the filepath standard library to extract the directory from the full path.

Here is an example code snippet demonstrating how to obtain the directory of the currently running file:

go
package main import ( "fmt" "os" "path/filepath" ) func main() { // Get the full path of the currently executing executable exePath, err := os.Executable() if err != nil { panic(err) } // Get the directory of the executable exeDir := filepath.Dir(exePath) fmt.Println("Current executable directory:", exeDir) }

In this example, we first call the os.Executable() function to retrieve the full path of the executable. If successful, it returns the path string and nil error. In case of an error, we terminate the program using panic. After obtaining the full path of the executable, we use the filepath.Dir() function to extract the directory containing the file.

When executing the above program, it will print the directory of the currently running executable. This is very useful for many applications that need to locate resource files relative to the executable, such as configuration files and templates.

2024年6月29日 12:07 回复

Use the osext package.

The function ExecutableFolder() returns the absolute path of the directory containing the executable file of the currently running program, which is particularly useful for cron jobs. It is cross-platform.

Online documentation

go
package main import ( "github.com/kardianos/osext" "fmt" "log" ) func main() { folderPath, err := osext.ExecutableFolder() if err != nil { log.Fatal(err) } fmt.Println(folderPath) }
2024年6月29日 12:07 回复

I transitioned from Node.js to Go. Equivalent to Node.js' __dirname in Go is:

go
_, filename, _, ok := runtime.Caller(0) if !ok { return errors.New("unable to get the current filename") } dirname := filepath.Dir(filename)

The thread also discusses other approaches and why they are incorrect:

  • os.Executable() provides the file path of the currently running executable. This is equivalent to process.argv[0] in Node.js. However, it differs from __dirname when obtaining the directory of a subpackage.
  • os.Getwd() returns the current working directory. This is equivalent to process.cwd() in Node.js. It will be incorrect if you run the program from a different directory.

Finally, I recommend avoiding third-party packages for this use case. Here is a self-contained package you can use:

go
package current // Filename is the __filename equivalent func Filename() (string, error) { _, filename, _, ok := runtime.Caller(1) if !ok { return "", errors.New("unable to get the current filename") } return filename, nil } // Dirname is the __dirname equivalent func Dirname() (string, error) { filename, err := Filename() if err != nil { return "", err } return filepath.Dir(filename), nil }

Note that I adjusted runtime.Caller(1) to 1 because we aim to retrieve the directory of the calling package current.Dirname(), not the directory containing the package current.

Update (12/23) As pointed out by @VonC below, when you run go build and distribute binary files, runtime.Caller(0) will still return your build directory. This may not align with your needs. This behavior differs from __dirname, which reflects the directory where the file resides. There is no perfect equivalent for this behavior in Go.

2024年6月29日 12:07 回复

你的答案