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
Yes, Finding package path is possible:
pathfind.go:
package main
/*
const char* GetMyPathFILE = __FILE__;
*/
import "C"
import "path/filepath"
var basepath = ""
//GetMyPath Returns the absolute directory of this(pathfind.go) file
func GetMyPath() string {
if basepath == "" {
g := C.GoString(C.GetMyPathFILE)
basepath = filepath.Dir(g)
}
return basepath
}
All you have to do is copy this file to your project. Keep in mind this comes up with the path for the file, not the caller so you have to copy the function/file to every project you need the function in. Additionally if you put this in a file with other code be sure to respect CGO
's import rules.