Add concurrency.
This commit is contained in:
parent
341054857e
commit
8b70410d58
18
main.go
18
main.go
|
@ -6,23 +6,31 @@ import (
|
|||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"sync"
|
||||
|
||||
"github.com/zeebo/blake3"
|
||||
)
|
||||
|
||||
var (
|
||||
help = flag.Bool("h", false, "Prints help information")
|
||||
length = flag.Int("l", 32, "The number of output bytes, prior to hex encoding (default 32)")
|
||||
length = flag.Int("l", 32, "The number of output bytes, prior to hex encoding")
|
||||
par = flag.Int("p", 4, "The maximum concurrency")
|
||||
)
|
||||
|
||||
func main() {
|
||||
flag.Parse()
|
||||
if *help || len(flag.Args()) == 0 {
|
||||
if *help || len(flag.Args()) == 0 || *par < 1 {
|
||||
flag.Usage()
|
||||
os.Exit(0)
|
||||
}
|
||||
buf := [65536]byte{}
|
||||
q := make(chan struct{}, *par)
|
||||
wg := &sync.WaitGroup{}
|
||||
|
||||
for _, name := range flag.Args() {
|
||||
q <- struct{}{}
|
||||
wg.Add(1)
|
||||
go func(name string) {
|
||||
buf := [65536]byte{}
|
||||
file, err := os.Open(name)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -42,5 +50,9 @@ func main() {
|
|||
fmt.Print(hex.EncodeToString(hash))
|
||||
fmt.Print(" ")
|
||||
fmt.Println(name)
|
||||
<- q
|
||||
wg.Done()
|
||||
}(name)
|
||||
}
|
||||
wg.Wait()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user