For now I used runtime.Caller(0)
in combination with path.Dir
and filepath.Abs
to get the path to the currently executed file and get the project root relative to it.
So lets say I've a folder structure like this:
$GOPATH/src/example.org/myproject
$GOPATH/src/example.org/myproject/main.go
$GOPATH/src/example.org/myproject/path
$GOPATH/src/example.org/myproject/path/loader.go
If I want my project root I call the loader.go which in turn gets its path with runtime.Caller(0)
and then goes up one folder to reach the project root.
The problem is when using go test -cover
the executed file is not in its normal place anymore but in a special sub directory for the testing and coverage analysis.
runtime.Caller(0)
gives me the following:
example.org/myproject/path/_test/_obj_/loader.go
Running it through path.Dir
and filepath.Abs
will give me:
$GOPATH/src/example.org/myproject/path/example.org/myproject/path/_test/_obj_
When I go up from there I won't reach the project root but something totally different obviously. So my questions stands:
Is there any reliable way of getting the project root?