Quantcast
Channel: User Sergey Ponomarev - Stack Overflow
Viewing all articles
Browse latest Browse all 58

Answer by Sergey Ponomarev for Golang: tests and working directory

$
0
0

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.


Viewing all articles
Browse latest Browse all 58

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>