Skip to content

Instantly share code, notes, and snippets.

@chressie
Created July 15, 2010 07:08
Show Gist options
  • Save chressie/476610 to your computer and use it in GitHub Desktop.
Save chressie/476610 to your computer and use it in GitHub Desktop.
traverse a file hierarchy in go
package main
import (
"container/vector"
"fmt"
"os"
"path"
)
type V struct {
list vector.StringVector
visited map[uint64]bool
}
func (v *V) VisitDir(path string, f *os.FileInfo) bool {
if _, ok := v.visited[f.Ino]; !ok {
v.visited[f.Ino] = true
return true
}
return false
}
func (v *V) VisitFile(p string, f *os.FileInfo) {
if f.Name == "PKGBUILD" {
v.list.Push(p[0 : len(p)-8]) // remove PKGBUILD from path and store it in vector
return
}
if g, err := os.Stat(p); err == nil && g.IsDirectory() {
d, _ := path.Split(p)
l, _ := os.Readlink(p)
path.Walk(path.Join(d, l), v, nil)
}
}
func main() {
v := new(V)
v.visited = make(map[uint64]bool)
path.Walk("tmp", v, nil)
for _, s := range v.list {
fmt.Println(s)
}
}
@sschober
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment