Get application parent dir

Hey there, I need to get application current dir and application PARENT dir, current dir was not hard and I did it as shown below;

	wd,err := os.Getwd()
	if err != nil {


How can I do it for parent dir? Eg;

Application dir is;

I need to get;

I’ve really searched a lot on Google and found no solution. Tnx and any help is appreciated.

Use filepath.Rel().

Be aware though that / has no parent and you will crash (as on the playground) if not caught before that.

PS: By “application dir” do you mean the folder that contains the executable or the folder that you are currently in?

1 Like

The executable folder I meant.

There is built-in way to find it.

You might be able to use os.Args[0] to check whether or not that is an absolut or relativ path or just the executable name. If its an absolute path, just use it, if it is relative, then use filepath.Rel(os.Getwd(), os.Args[0]) or if it is just the name, then you’ll need to search all entries in the PATH environment variable if there is an executable file with the name learnt from os.Args[0].

But why do you actually need that path? A good executable doesn’t care about where it lives.

I use in my projects this piece of code

path, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {

This allow me to execute my program from everywhere. Also i use filepath.Join(path, filename) to access safe other files from the application subfolders.

LE: I guess is not a good idea to use relative paths :thinking:. I think about running your application from crontab where absolute path counts.


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.