Home »
Golang »
Golang FAQ
How to get the directory of the currently running file in Golang?
Here, we have to get the directory of the currently running file.
Submitted by IncludeHelp, on December 05, 2021
To get the directory of the currently running file – We use the Executable() function of the os package. This function accepts nothing and returns a string containing the path name for the executable that started the current process. and return an error if any. And, then Dir() function of the path/filepath package.
Syntax:
func Executable() (string, error)
Program:
package main
import (
"fmt"
"os"
"path/filepath"
)
func main() {
// Getting the path name for the executable
// that started the current process.
pathExecutable, err := os.Executable()
if err != nil {
panic(err)
}
// Getting the directory path/name
dirPathExecutable := filepath.Dir(pathExecutable)
fmt.Println("Directory of the currently running file...")
fmt.Println(dirPathExecutable)
}
Output:
Directory of the currently running file...
/tmpfs
Golang FAQ »