Suppose we have the following folder structure:
|-- Repo
| |-- Data
| | |-- Sets
| | | |-- Set1.json
| |-- Tests
| |---|-- Case1
| |---|---|-- main.go
If I want to access the Set1.json file, how would I do that from main.go? Suppose I’m allowed to provide the relative path with respective to repo, i.e. Repo/Data/Sets
would be provided. Is there a way for me to immediately figure out the absolute path involving that relative path on any system?
My solution was using os.Getwd
to get the current location and then use:
tmp := filepath.Dir(filepath.Dir(currDir))
To get to the sibling of the parent of the folder where main.go
was. Then I just do:
res := filepath.Join(tmp, "Data/Sets")
Or
res := filepath.Join(tmp, "Data\Sets")
And this would get me the absolute path to the Sets folder. Is there a smarter way to get there somehow? It should work on any system.