Golang get current directory, cwd, and path: os.Getwd, Getwd, Executable

Golang get current directory and golang get cwd: os.Getwd for golang get current working directory and golang get pwd, golang print current directory, golang get current path with filepath.Abs, go get current directory vs executable path with os.Executable, and golang get path notes.

Published

Updated

Read time 3 min read

Reviewed byDeepak Prasad

Golang get current directory, cwd, and path: os.Getwd, Getwd, Executable

People searching golang get current directory, golang get cwd, go get current directory, golang get current working directory, golang get pwd, go get cwd, golang get working directory, golang print current directory, or golang get current path usually want one of two things: the process current working directory (like the shell pwd), or the directory of the running binary. In Go the cwd comes from os.Getwd; the executable path comes from os.Executable plus path/filepath.Dir. This guide shows both, when they differ, and how filepath.Abs answers golang get path for relative names.

Tested with Go 1.24 on Linux.


Golang get cwd / pwd: os.Getwd

os.Getwd returns the current working directory of your process—the same notion as golang get pwd or golang get cwd in other languages.

go
package main

import (
	"fmt"
	"os"
)

func main() {
	wd, err := os.Getwd()
	if err != nil {
		panic(err)
	}
	fmt.Println(wd)
}
Output

Running go run from /tmp prints /tmp (your output will match wherever you run the program).

Golang print current directory

Use fmt.Println(wd) or log.Println(wd) after a successful Getwd, as in the example above.


Golang get current path: filepath.Abs(".")

To turn . into an absolute path (still based on the cwd), use filepath.Abs:

go
package main

import (
	"fmt"
	"path/filepath"
)

func main() {
	dir, err := filepath.Abs(".")
	if err != nil {
		panic(err)
	}
	fmt.Println(dir)
}
Output

This is a common golang get current path pattern for config files or assets resolved relative to where the user launched the program.


Executable directory: os.Executable and filepath.Dir

Go get current directory wording sometimes means “where is my binary?”—that is not always the cwd. Use os.Executable and take the directory with filepath.Dir:

go
package main

import (
	"fmt"
	"os"
	"path/filepath"
)

func main() {
	ex, err := os.Executable()
	if err != nil {
		panic(err)
	}
	fmt.Println("executable:", ex)
	fmt.Println("exe dir:", filepath.Dir(ex))
}
Output

Symlinks and go run build paths can make this differ from a production install; see the docs and consider filepath.EvalSymlinks if you need a stable path.

Avoid relying on os.Args[0] alone for security or correctness: it can be changed by the caller and may be relative.


Source file directory: runtime.Caller

If you need the directory of this Go source file (for generators or tests), runtime.Caller gives a file path you can pass to filepath.Dir:

go
package main

import (
	"fmt"
	"path/filepath"
	"runtime"
)

func main() {
	_, file, _, ok := runtime.Caller(0)
	if !ok {
		panic("caller")
	}
	fmt.Println(filepath.Dir(file))
}
Output

That prints the folder containing the compiled file’s path (under go run it lives under the build cache).


Summary

For golang get current directory in the cwd / pwd sense, use os.Getwd() and handle the error. For golang get current path from a relative name, use filepath.Abs. For “where is the binary?” use os.Executable with filepath.Dir, not os.Args[0] by itself. runtime.Caller is for source locations. Keeping cwd, absolute paths, and executable layout straight prevents subtle bugs when users start your program from different folders—see goroutines for concurrency basics if you combine directory discovery with background work.


References


Frequently Asked Questions

1. How do I golang get cwd or golang get pwd?

Call os.Getwd(); it returns the process current working directory and an error, which is the usual answer for golang get working directory and go get cwd.

2. What is the difference between golang get current directory and the executable path?

os.Getwd() is the shell-style cwd; os.Executable() returns the path of the binary that started the process—the directory containing the binary is often different from the cwd when you launch a program with a relative path.

3. How do I golang print current directory?

Use fmt.Println (or log) with the string from os.Getwd() after checking the error.

4. When should I use filepath.Abs for golang get current path?

Use filepath.Abs(".") or filepath.Abs(relativePath) when you need an absolute path built from the cwd; it resolves . and .. for the current OS.

5. Is github.com/kardianos/osext still needed?

Rarely: os.Executable plus filepath.Dir covers most executable-directory cases; third-party helpers were more common before those APIs matured.
Tuan Nguyen

Data Scientist

Proficient in Golang, Python, Java, MongoDB, Selenium, Spring Boot, Kubernetes, Scrapy, API development, Docker, Data Scraping, PrimeFaces, Linux, Data Structures, and Data Mining. With expertise …