Skip to content

Instantly share code, notes, and snippets.

@Veejay
Created January 17, 2013 07:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Veejay/4554436 to your computer and use it in GitHub Desktop.
Save Veejay/4554436 to your computer and use it in GitHub Desktop.
package main
import (
"fmt"
"net/http"
"strings"
"log"
"io/ioutil"
"path/filepath"
"os"
)
func WalkPathRecursively(path string, filepaths chan string, done chan bool) {
dir, err := os.Open(path)
defer dir.Close()
if err != nil {
// Do something with error here
}
contents, err := dir.Readdir(0)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
for _, f := range contents {
if f.IsDir() {
WalkPathRecursively(filepath.Join(path, f.Name()), filepaths, done)
} else {
filepaths <- filepath.Join(path, f.Name())
}
}
close(filepaths)
done <- true
}
func main() {
dirName := "/some/path"
filepaths := make(chan string)
done := make(chan bool)
WalkPathRecursively(dirName, filepaths, done)
for {
select {
case msg := <-done:
close(done)
goto END
case msg, _ := <-filepaths:
fmt.Println(msg)
}
}
// Not sure if I need this
END:
return
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment