Copy subdirectories concurrently

152 views
Skip to first unread message

Adrian Stanciu

unread,
Jan 30, 2014, 3:26:24 PM1/30/14
to golan...@googlegroups.com
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 ?

Dan Kortschak

unread,
Jan 30, 2014, 3:43:57 PM1/30/14
to Adrian Stanciu, golan...@googlegroups.com
f doesn't bind necessarily until after the loop completes. Add f := f after the for line or make the goroutine func take an argument.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Adrian Stanciu

unread,
Jan 30, 2014, 3:50:59 PM1/30/14
to golan...@googlegroups.com, Adrian Stanciu
Thanks, that did the trick.

Dave Cheney

unread,
Jan 30, 2014, 5:10:20 PM1/30/14
to Adrian Stanciu, golan...@googlegroups.com


On 31 Jan 2014, at 7:26, Adrian Stanciu <adrians...@gmail.com> wrote:

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)

Err is lost here
file, err := files.Readdir(0)

Don't assume the state if files here, it could be nil and your program may panic. 

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 ?

--
Reply all
Reply to author
Forward
0 new messages