title: Copy subdirectories using goroutines
body: <p>My program copies multiple files and directories from different parts of the computer to one place.  </p>
  
  <p>One of the directories is very big, so it takes about 20-30 seconds to copy it. For now I just made this method which copies that directory to start as a goroutine:</p>
  
  <pre><code>func CopySpecificDirectory(source, dest string, quit chan int) (err error) {
      files, err := os.Open(source)
      file, err := files.Readdir(0)
  
      if err != nil {
          fmt.Printf("Error reading directory %s: %s\n", source, err)
          return err
      }
  
      for _, f := range file {
          if f.IsDir() {
              copy.CopyDir(source+"\\"+f.Name(), dest+"\\"+f.Name())
          } else {
              copy.CopyFile(source+"\\"+f.Name(), dest+"\\"+f.Name())
          }
      }
  
      quit &lt;- 1
  
      return nil
  }
  </code></pre>
  
  <p>Main:  </p>
  
  <pre><code>quit := make(chan int)
  go CopySpecificDirectory(config.Location+"\\Directory", config.Destination, quit)
  </code></pre>
  
  <p>This just improves my program by a few seconds. Inside my CopySpecificDirectory method (if this is the best way) I want for each directory to create a goroutine, something like this maybe:</p>
  
  <pre><code>c := make(chan int)
  for _, f := range file {
      if f.IsDir() {
          go func() {
              copy.CopyDir(source+"\\"+f.Name(), dest+"\\"+f.Name())
              c &lt;- 1
          }()
      } else {
          copy.CopyFile(source+"\\"+f.Name(), dest+"\\"+f.Name())
      }
  }
  </code></pre>
  
  <p>With this approach I don't know where to wait for the copy to finish for every directory (&lt;- c).<br>
  Is this the best way ? If anyone has other suggestion what is the fastest way to copy a directory, I will love to hear it.</p>
  
  <p>edit:</p>
  
  <p>I used the aproach form the example of sync.WaitGroup from the website.  </p>
  
  <pre><code>for _, f := range file {
      if f.IsDir() {
          wg.Add(1)
          go func() {
              defer wg.Done()
              copy.CopyDir(source+"\\"+f.Name(), dest+"\\"+f.Name())
          }()
      // more code
  </code></pre>
  
  <p>I have declared <code>var wg sync.WaitGroup</code> as global, and I do <code>wg.Wait()</code> in main right after I call <code>CopySpecificDirectory</code>.</p>
  
  <p>But <code>CopySpecificDirectory</code> finishes before copying all the contents. What am I doing wrong ? Looks like it is not waiting for the goroutines to finish.</p>