Skip to content

Instantly share code, notes, and snippets.

@grom358 grom358/files.go
Created Aug 25, 2015

Embed
What would you like to do?
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"sync"
)
func main() {
flag.Parse()
root := flag.Arg(0)
files := make(chan string, 4)
walker := func(fp string, fi os.FileInfo, err error) error {
if err != nil {
return nil
}
if fi.IsDir() {
return nil
}
/*
matched, err := filepath.Match("*.mp3", fi.Name())
if err != nil {
fmt.Println(err) // malformed pattern
return err // this is fatal.
}
if matched {
files <- fp
}
*/
files <- fp
return nil
}
go func() {
filepath.Walk(root, walker)
close(files)
}()
var wg sync.WaitGroup
wg.Add(4)
for i := 0; i < 4; i++ {
go func() {
defer wg.Done()
for file := range files {
fmt.Println(file)
}
}()
}
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.