Here is function to find a root project directory that contains .git folder:
import ("os""path/filepath")func FindProjectDir() string { // Test can be started from command line in project folder or in IDE // Try to detect a correct path to a project directory // Find a project dir that contains .git curDir, _ := os.Getwd() projectDir := curDir for { _, err := os.Stat(projectDir +"/.git") if err == nil { return projectDir } projectDir = filepath.Dir(projectDir) if projectDir == "/" { // reached root return "" } }}
You can adapt it and append the file name that you are looking for.Still I think this answer is better https://stackoverflow.com/a/71488632/1049542But sometime you need also to execute some shell script in the project root folder so you need to know the parent folder.