SHA directory wait group

This commit is contained in:
Meutel 2017-07-08 15:32:40 +02:00
parent 865b0a1c88
commit 01518f6f9f

View File

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