title: GoLang Printing a Directory Tree
body: <p>I'm having trouble creating a recursive function in Go to print a directory with all its subdirectories and files. I got some issues with saving the current files or directory to the result. The following code is only printing level 0 directories.</p>
  
  <pre class="lang-golang prettyprint-override"><code>func ListTree(path string, level int) (result string, err error) {
      files, err := ioutil.ReadDir(path)
      if err != nil {
          return result, err
      }
      for idx, entry := range files {
          result += fmt.Sprintf("\n%v/%v", MultiplyElement("\t", level), entry.Name())
          if entry.IsDir() {
              ListTree(path + "/" + entry.Name(),level+1)
          }
      }
      return result, err
  }
  </code></pre>
  
  <p>However, when I declare the result variable outside the function (I know that's bad code, just for testing) it works perfectly, but the result value isn't cleared after calling the function. </p>
  
  <pre class="lang-golang prettyprint-override"><code>var result string
  func ListTree(path string, level int) (string, error) {
      files, err := ioutil.ReadDir(path)
      if err != nil {
          return result, err
      }
      for idx, entry := range files {
          result += fmt.Sprintf("\n%v/%v", MultiplyElement("\t", level), entry.Name())
          if entry.IsDir() {
              ListTree(path + "/" + entry.Name(),level+1)
          }
      }
      return result, err
  }
  </code></pre>
  
  <p>How could I fix this issue?</p>