title: Is it possible to get the current root of package structure as a string in golang test?
body: <p>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). Here are three solutions which do not work, to explain what I am looking for and why.</p>
  
  <ol>
  <li><p>Hardcode the absolute path. This fails because another user who is trying to test the project might have a different prefix on the absolute path.</p></li>
  <li><p>Hardcode a relative path from the path of the file which defines the utility function. This fails because packages which import and use this function are not necessarily at the same level of the file hierarchy as the file that defines the utility function, and relative paths are interpreted relative to the importer, not the imported.</p></li>
  <li><p>Pass in the relative path to the file from every caller relative to the caller's package. This actually works but seems to be very verbose because now every caller must be changed to pass one file.</p></li>
  </ol>
  
  <p>I see a fourth solution, whereby I can hardcode a path in the utility function which is relative to the root directory of the top-level package. However, I have not been able to find a way to get the root directory in code, although I suspect there is one because imports can be resolved from the root.</p>
  
  <p><strong>Thus, how might I get the sought-after root directory?</strong></p>
  
  <p>I've looked over various Go documents but have so far failed to find a solution. I have also seen <a href="https://stackoverflow.com/questions/17071286/how-can-i-open-files-using-relative-paths-in-go">this question</a> but the solution there is equivalent to #3 above.</p>