how to reference a relative file from code and tests

go test working directory
golang relative import

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:

  1. Command line flag (see flag package)
  2. Environment variable (see os.Getenv())
  3. (Fix named) Config file in user's home directory (see 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"

go - how to reference a relative file from code and tests, Relative paths are always interpreted / resolved to a base path: the current or working directory - therefore it will always have its limitations. Select the first empty cell in Column D (D16) and type = COUNTA (D2:D15). On the Developer tab, click Stop Recording to stop recording the macro. At this point, you’ve recorded two macros. Take a moment to examine the code for your newly created macro by selecting Macros on the Developer tab to open the Macro dialog box.

Internet Technologies and Information Services, 2nd Edition, This technique can create links to other places within the same file providing Making most references relative simplifies the source code, facilitates testing of  File path formats on Windows systems. 06/06/2019; 15 minutes to read +4; In this article. Members of many of the types in the System.IO namespace include a path parameter that lets you specify an absolute or relative path to a file system resource.

Altova® MapForce® 2010 User & Reference Manual, Relative file names for instance files are therefore resolved relative to the execution file C++, C#, Java Working directory when running the generated code  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.

File path formats on Windows systems, Publications\TravelBrochure.pdf, A relative path to file in a directory For example, the following code renames a file named test.txt to Test.txt:. I´m using Visual studio 2013 and the chutzpah plugin with jasmine to test my angularjs code and I struggled for hours to get the tests running in the Test Explorer, it all turned out to be a problem with getting the references correct. How to configure chutzpah paths to find the files in a different project than your test project.

Code of Federal Regulations: LSA, list of CFR sections affected, The relative accuracy test results are also acceptable if the difference between measurements and the corresponding reference method measurement mean as an ASCII flat file capable of transmission both by direct computer-to-computer​  A relative reference in Excel is a pointer to a cell or range of cells. For example, a relative reference to cell A1 looks like this: =A1 A relative addresses will change when copied to other location in a worksheet because it describes the "offset"

The Code of Federal Regulations of the United States of America, (b) For affected units where the average of the flow reference method measurements of The relative accuracy test results are also acceptable if the difference as an ASCII flat file capable of transmission both by direct computer-​to-computer  To illustrate for how to read file and resource in JUnit test, in the above example, let’s assume that the FileUtilsTest test class in the test sources (src/test/java) will need to read a file called lorem_ipsum.txt in the test resources (src/test/resources). 2. Read File and Resource in JUnit Test Examples 2.1. Using ClassLoader’s Resource

Comments
  • is there a way to know the path of the .go file?
  • @PabloFernandez If it is compiled into an executable binary, there isn't even a .go file...
  • On Linux, if /proc is mounted, it's possible to know the location of the program being executed by calling os.Readlink() on the symlink named "/proc/self/exe". In either case, it's a) not portable; b) may have little sense anyway as a properly packaged software package for a GNU/Linux-based system should have its binaries and "assets" separated anyway.
  • I'd mention another possibility as well: 1) have a global variable defining the base directory prefix for the assets, say, main.AssetsDir; 2) allow downstream packagers override it at build time using the linker's ability to override string-typed values, like with: go build -ldflags="-X main.AssetsDir=/usr/share/cool_app/assets". The OP could get more background on this by studying the outputs of go help build and go tool link -h.
  • @kostix Good idea, but that limits the configurability to developers. Users without programming knowledge would find command line flags much friendlier and easier to use than build flags.
  • Not sure why but both BasePath and AppPath return the empty string :/
  • @PabloFernandez did you revel.Init()?
  • The link is dead. The new one seems to be https://revel.github.io/