Skip to content

Instantly share code, notes, and snippets.

@DocSavage
Last active August 18, 2017 17:16
Show Gist options
  • Save DocSavage/4529160 to your computer and use it in GitHub Desktop.
Save DocSavage/4529160 to your computer and use it in GitHub Desktop.
Nice little DAG walking in Go language from http://golang.org/src/cmd/go/pkg.go
// packageList returns the list of packages in the dag rooted at roots
// as visited in a depth-first post-order traversal.
func packageList(roots []*Package) []*Package {
seen := map[*Package]bool{}
all := []*Package{}
var walk func(*Package)
walk = func(p *Package) {
if seen[p] {
return
}
seen[p] = true
for _, p1 := range p.imports {
walk(p1)
}
all = append(all, p)
}
for _, root := range roots {
walk(root)
}
return all
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment