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() }