I'm having trouble resolving relative file paths in my Go application. For this app I decided to make a package that offers a uniform interface to the different configuration files. The conf
package contains the relevant data files, so this is basically the file tree:
app/conf
+ config.go
+ config.json
+ ...
app/code
+ code.go
+ code_test.go
Problem is, when the tests defined in app/code/code_test.go
calls a function in the app/conf
package, which in turn tries to open app/conf/config.json
, the relative path is messed up since the working directory is at app/code
.
I've looked at other SO answers that mentions the
path/filepath
package and especially thefilepath.Abs
function for turning relative paths to absolute paths. However, this won't solve my problem since the absolute path will be based on the wrong working directory.Some solution with "absolute paths" based on GOPATH would probably suffice, but I imagine the GOPATH would have little meaning when the code is built and exported.
Simply porting all configuration files to hard coded Go structs is not viable either as they're used cross-language.