Created
January 17, 2013 07:56
-
-
Save Veejay/4554436 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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