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?
File paths are a common stumbling block for novice programmers. First, what’s the difference between a relative file path and an absolute file path? A relative path describes the location of a file relative to the current (working) directory*. An absolute path describes the location from the root directory.
One of the choices is 'Add As Link'. This will add the file to your project without copying it. On the file properties you can choose 'Copy if newer' for 'Copy to Output Directory'. You can then consume the file in your test without maintaining two copies.
You have mixed up Resources in Applications, which are usually embedded in an assembly, with loose files your application can load at runtime using a relative path. I think I understood how you get here, hence another post. Let me try to summarize these two issues one last time for you:
A relative path describes the location of a file relative to the current (working) directory*. An absolute path describes the location from the root directory. When learning to access data files through programming, we regularly use relative file paths. In these Java examples, we define a path to the EQs_last_last_week_of_2021.csv file.
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
and os/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()
or filepath.Join()
, e.g.:
// Get base path, from any or from the combination of the above mentioned solutions
base := "/var/myapp"
// Relative path, resource to read/write from:
relf := "conf/patients.json"
// Full path that identifies the resource:
full := filepath.Join(base, relf) // full will be "/var/myapp/conf/patients.json"
I've never used Revel myself but the following looks helpful to me:
http://revel.github.io/docs/godoc/revel.html
revel.BasePath
revel.AppPath
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With