I need to reference patients.json
from patients.go
, here's the folder structure:
If I do:
filepath.Abs("../../conf/patients.json")
it works for go test ./...
but fails for revel run
If I do:
filepath.Abs("conf/patients.json")
the exact opposite happens (revel is fine but tests fail).
Is there a way to correctly reference the file so that it works both for tests and normal program run?
Relative paths are always interpreted / resolved to a base path: the current or working directory - therefore it will always have its limitations.
If you can live with always taking care of the proper working directory, you may keep using relative paths.
What I would suggest is to not rely on the working directory, but an explicitly specified base path. This may have a default value hard-coded in your application (which may be the working directory as well), and you should provide several ways to override its value.
Recommended ways to override the base path to which your "relative" paths are resolved against:
flag
package)os.Getenv()
)os/user/User
andos/user/Current()
)Once you have the base path, you can get the full path by joining the base path and the relative path. You may use
path.Join()
orfilepath.Join()
, e.g.: