Find current working directory
path, err := os.Executable()
if err != nil {
dir := filepath.Dir(path)
fmt.Println(path) // for example /home/user/main
fmt.Println(dir)  // for example /home/user
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 this page: