Skip to content

Instantly share code, notes, and snippets.

@miku
Forked from DocSavage/gist:4529160
Last active August 29, 2015 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miku/abf4c5f6a04ccbcfdf17 to your computer and use it in GitHub Desktop.
Save miku/abf4c5f6a04ccbcfdf17 to your computer and use it in GitHub Desktop.
// 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