| 1 | // Copyright 2014 The Go Authors. All rights reserved. |
|---|---|
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package typeutil |
| 6 | |
| 7 | import "go/types" |
| 8 | |
| 9 | // Dependencies returns all dependencies of the specified packages. |
| 10 | // |
| 11 | // Dependent packages appear in topological order: if package P imports |
| 12 | // package Q, Q appears earlier than P in the result. |
| 13 | // The algorithm follows import statements in the order they |
| 14 | // appear in the source code, so the result is a total order. |
| 15 | func Dependencies(pkgs ...*types.Package) []*types.Package { |
| 16 | var result []*types.Package |
| 17 | seen := make(map[*types.Package]bool) |
| 18 | var visit func(pkgs []*types.Package) |
| 19 | visit = func(pkgs []*types.Package) { |
| 20 | for _, p := range pkgs { |
| 21 | if !seen[p] { |
| 22 | seen[p] = true |
| 23 | visit(p.Imports()) |
| 24 | result = append(result, p) |
| 25 | } |
| 26 | } |
| 27 | } |
| 28 | visit(pkgs) |
| 29 | return result |
| 30 | } |
| 31 |
Members