github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/types/typeutil/imports.go (about)

     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  //
    16  func Dependencies(pkgs ...*types.Package) []*types.Package {
    17  	var result []*types.Package
    18  	seen := make(map[*types.Package]bool)
    19  	var visit func(pkgs []*types.Package)
    20  	visit = func(pkgs []*types.Package) {
    21  		for _, p := range pkgs {
    22  			if !seen[p] {
    23  				seen[p] = true
    24  				visit(p.Imports())
    25  				result = append(result, p)
    26  			}
    27  		}
    28  	}
    29  	visit(pkgs)
    30  	return result
    31  }