refactoring SHA dir: close channels

This commit is contained in:
Meutel 2017-07-08 17:15:37 +02:00
parent 78d5ca23a3
commit e75afaf5e6

View File

@ -28,34 +28,34 @@ func main() {
chan_hash, chan_path := make(chan string), make(chan string) chan_hash, chan_path := make(chan string), make(chan string)
for i := 0; i < 100; i++ { for i := 0; i < 100; i++ {
wg.Add(1)
go func() { go func() {
for path := range chan_path { for path := range chan_path {
if h, err := sha256File(path); err != nil { if h, err := sha256File(path); err != nil {
wg.Done()
log.Print("Error "+path, err) log.Print("Error "+path, err)
} else { } else {
chan_hash <- h chan_hash <- h
} }
} }
wg.Done()
}() }()
} }
go func() { go func() {
for h := range chan_hash { wg.Wait()
fmt.Println(h) close(chan_hash)
wg.Done()
}
}() }()
err := filepath.Walk(os.Args[1], func(path string, info os.FileInfo, err error) error { go func() {
if err == nil && !info.IsDir() { filepath.Walk(os.Args[1], func(path string, info os.FileInfo, err error) error {
wg.Add(1) if err == nil && !info.IsDir() {
chan_path <- path chan_path <- path
} }
return err return err
}) })
if err != nil { close(chan_path)
log.Fatalln(err) }()
for h := range chan_hash {
fmt.Println(h)
} }
wg.Wait()
} }