-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
54 lines (45 loc) · 936 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package main
import "fmt"
type Package struct {
name string
deps []Package
}
func main() {
/*
"A" : ["D", "B"]
"B" : ["C", "E"]
"C" : ["D", "E"]
*/
D := addPackage("D")
E := addPackage("E")
C := addPackage("C", D, E)
B := addPackage("B", C, E)
A := addPackage("A", D, B)
packages := getDeps(A)
fmt.Printf("Package: %v depends on packages: %v\n", A.name, packages)
}
func addPackage(name string, deps ...Package) Package {
newPackage := Package{
name: name,
deps: deps,
}
return newPackage
}
func getDeps(p Package) []string {
var result []string
var names = make(map[string]bool)
ret := helper(p, result, names)
return ret
}
func helper(dep Package, result []string, names map[string]bool) []string {
for _, x := range dep.deps {
if !names[x.name] {
names[x.name] = true
result = append(result, x.name)
if x.deps != nil {
return helper(x, result, names)
}
}
}
return result
}