vendredi 21 juillet 2017

How to reliably get the projects root in go?

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/http://ift.tt/2tN8Gwx
$GOPATH/src/http://ift.tt/2uI3uOR
$GOPATH/src/http://ift.tt/2tMy7hZ
$GOPATH/src/http://ift.tt/2uI3vlT

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:

http://ift.tt/2tN40GR

Running it through path.Dir and filepath.Abs will give me:

$GOPATH/src/http://ift.tt/2uI3vSV

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?

Aucun commentaire:

Enregistrer un commentaire