8

Find current working directory

 3 years ago
source link: https://yourbasic.org/golang/current-directory/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Find current working directory

yourbasic.org/golang
directions.jpg

Current directory

Use os.Getwd to find the path name for the current directory.

path, err := os.Getwd()
if err != nil {
    log.Println(err)
}
fmt.Println(path)  // for example /home/user

Warning: If the current directory can be reached via multiple paths (due to symbolic links), Getwd may return any one of them.

Current executable

Use os.Executable to find the path name for the executable that started the current process.

path, err := os.Executable()
if err != nil {
    log.Println(err)
}
fmt.Println(path) // for example /tmp/go-build872132473/b001/exe/main

Warning: There is no guarantee that the path is still pointing to the correct executable. If a symlink was used to start the process, depending on the operating system, the result might be the symlink or the path it pointed to. If a stable result is needed, path/filepath.EvalSymlinks might help.

Share:             


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK