My program copies multiple files and directories from different parts of the computer to one place.
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:
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
", 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 <- 1
return nil
}
Main:
quit := make(chan int)
go CopySpecificDirectory(config.Location+"\\Directory", config.Destination, quit)
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:
c := make(chan int)
for _, f := range file {
if f.IsDir() {
go func() {
copy.CopyDir(source+"\\"+f.Name(), dest+"\\"+f.Name())
c <- 1
}()
} else {
copy.CopyFile(source+"\\"+f.Name(), dest+"\\"+f.Name())
}
}
With this approach I don't know where to wait for the copy to finish for every directory (<- c).
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.
edit:
I used the aproach form the example of sync.WaitGroup from the website.
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
I have declared var wg sync.WaitGroup
as global, and I do wg.Wait()
in main right after I call CopySpecificDirectory
.
But CopySpecificDirectory
finishes before copying all the contents. What am I doing wrong ? Looks like it is not waiting for the goroutines to finish.