Last active
August 18, 2017 17:16
-
-
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
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
// 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