From e75afaf5e6691eb14fd2a973d949c897842b7485 Mon Sep 17 00:00:00 2001 From: Meutel Date: Sat, 8 Jul 2017 17:15:37 +0200 Subject: [PATCH] refactoring SHA dir: close channels --- shadirconcurrent/main.go | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/shadirconcurrent/main.go b/shadirconcurrent/main.go index e673806..ac5a975 100644 --- a/shadirconcurrent/main.go +++ b/shadirconcurrent/main.go @@ -28,34 +28,34 @@ func main() { chan_hash, chan_path := make(chan string), make(chan string) for i := 0; i < 100; i++ { + wg.Add(1) go func() { for path := range chan_path { if h, err := sha256File(path); err != nil { - wg.Done() log.Print("Error "+path, err) } else { chan_hash <- h } } + wg.Done() }() } - go func() { - for h := range chan_hash { - fmt.Println(h) - wg.Done() - } + wg.Wait() + close(chan_hash) }() - err := filepath.Walk(os.Args[1], func(path string, info os.FileInfo, err error) error { - if err == nil && !info.IsDir() { - wg.Add(1) - chan_path <- path - } - return err - }) - if err != nil { - log.Fatalln(err) + go func() { + filepath.Walk(os.Args[1], func(path string, info os.FileInfo, err error) error { + if err == nil && !info.IsDir() { + chan_path <- path + } + return err + }) + close(chan_path) + }() + + for h := range chan_hash { + fmt.Println(h) } - wg.Wait() }