Created
August 4, 2011 20:52
-
-
Save kylelemons/1126245 to your computer and use it in GitHub Desktop.
A simple file list program
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 ( | |
"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