From 01518f6f9f0f0ef23c4b4fbbc733799e3069c6c6 Mon Sep 17 00:00:00 2001 From: Meutel Date: Sat, 8 Jul 2017 15:32:40 +0200 Subject: [PATCH] SHA directory wait group --- shadirconcurrent/main.go | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/shadirconcurrent/main.go b/shadirconcurrent/main.go index a03894a..03bfa48 100644 --- a/shadirconcurrent/main.go +++ b/shadirconcurrent/main.go @@ -6,9 +6,10 @@ import "io" import "log" import "os" import "path/filepath" -import "time" +import "sync" var chan_hash chan string +var wg sync.WaitGroup func walk(path string, info os.FileInfo, err error) error { if err != nil { @@ -19,6 +20,7 @@ func walk(path string, info os.FileInfo, err error) error { if info.IsDir() { return nil } else { + wg.Add(1) go hash(info.Name(), path) } return nil @@ -44,21 +46,25 @@ func sha256File(path string) ([]byte, error) { return h.Sum(nil), nil } +func printer() { + for { + select { + case str := <-chan_hash: + fmt.Println(str) + wg.Done() + } + } +} + func main() { if len(os.Args) < 2 { log.Fatalln("Usage shadir file") } chan_hash = make(chan string) + + go printer() if err := filepath.Walk(os.Args[1], walk); err != nil { panic(err) } - - for { - select { - case str := <-chan_hash: - fmt.Println(str) - case <-time.After(10 * time.Millisecond): - return - } - } + wg.Wait() }