Skip to content

Instantly share code, notes, and snippets.

@kylelemons
Created August 4, 2011 20:52
Show Gist options
  • Save kylelemons/1126245 to your computer and use it in GitHub Desktop.
Save kylelemons/1126245 to your computer and use it in GitHub Desktop.
A simple file list program
package main
import (
"path/filepath"
"fmt"
"log"
"os"
)
type visitor struct {
root string
files []string
dirs int
}
func (v *visitor) run() {
errors := make(chan os.Error)
defer close(errors)
go func() {
for err := range errors {
log.Printf("walk: %s", err)
}
}()
filepath.Walk(v.root, v, errors)
}
func (v *visitor) VisitDir(path string, f *os.FileInfo) bool {
v.dirs += 1
return true
}
func (v *visitor) VisitFile(path string, f *os.FileInfo) {
v.files = append(v.files, path)
}
func main() {
v := visitor{root: "."}
if len(os.Args) > 1 {
v.root = os.Args[1]
}
v.run()
for _, path := range v.files {
fmt.Println(path)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment