I am writing a utility function for my unit tests which is used by unit tests in multiple packages. This utility function must read a particular file (always the same file). Her
Building on the answer by Oleksiy you can create a sub-package in your project called ./internal/projectpath/projectpath.go
and paste in the following:
package projectpath
import (
"path/filepath"
"runtime"
)
var (
_, b, _, _ = runtime.Caller(0)
// Root folder of this project
Root = filepath.Join(filepath.Dir(b), "../..")
)
Then you can use projectpath.Root
in any other package to have the root folder of your project.