I am trying to achieve this using sync.WaitGroups based on the example from
golang.org sync package.
var wg sync.WaitGroup // this is global
func CopyWebFiles(source, dest string) (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() {
wg.Add(1)
go func() {
defer wg.Done()
copy.CopyDir(source+"\\"+f.Name(), dest+"\\"+f.Name())
}()
} else {
copy.CopyFile(source+"\\"+f.Name(), dest+"\\"+f.Name())
}
}
return nil
}
func main() {
CopyWebFiles(config.Location+"\\WebFiles", config.Destination)
wg.Wait()
}
But the problem is it doesn't copy only one directory instead of 9. If I print the directory copied before defer wg.Done() it says the same directory, so does that means that copies the same directory 9 times ?
I don't really understand what my problem is, can someone help me figure it out ?