// Copyright 2018 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package packagesimport ()// Visit visits all the packages in the import graph whose roots are// pkgs, calling the optional pre function the first time each package// is encountered (preorder), and the optional post function after a// package's dependencies have been visited (postorder).// The boolean result of pre(pkg) determines whether// the imports of package pkg are visited.//// Example://// pkgs, err := Load(...)// if err != nil { ... }// Visit(pkgs, nil, func(pkg *Package) {// log.Println(pkg)// })//// In most cases, it is more convenient to use [Postorder]://// for pkg := range Postorder(pkgs) {// log.Println(pkg)// }func ( []*Package, func(*Package) bool, func(*Package)) { := make(map[*Package]bool)varfunc(*Package) = func( *Package) {if ![] { [] = trueif == nil || () {for , := rangesorted(.Imports) { // for determinism () } }if != nil { () } } }for , := range { () }}// PrintErrors prints to os.Stderr the accumulated errors of all// packages in the import graph rooted at pkgs, dependencies first.// PrintErrors returns the number of errors printed.func ( []*Package) int {varint := make(map[*Module]bool)for := rangePostorder() {for , := range .Errors {fmt.Fprintln(os.Stderr, ) ++ }// Print pkg.Module.Error once if present. := .Moduleif != nil && .Error != nil && ![] { [] = truefmt.Fprintln(os.Stderr, .Error.Err) ++ } }return}// Postorder returns an iterator over the the packages in// the import graph whose roots are pkg.// Packages are enumerated in dependencies-first order.func ( []*Package) iter.Seq[*Package] {returnfunc( func(*Package) bool) { := make(map[*Package]bool)varfunc(*Package) bool = func( *Package) bool {if ![] { [] = truefor , := rangesorted(.Imports) { // for determinismif !() {returnfalse } }if !() {returnfalse } }returntrue }for , := range {if !() {break } } }}// -- copied from golang.org.x/tools/gopls/internal/util/moremaps --// sorted returns an iterator over the entries of m in key order.func [ ~map[], cmp.Ordered, any]( ) iter.Seq2[, ] {// TODO(adonovan): use maps.Sorted if proposal #68598 is accepted.returnfunc( func(, ) bool) { := keySlice()slices.Sort()for , := range {if !(, []) {break } } }}// KeySlice returns the keys of the map M, like slices.Collect(maps.Keys(m)).func [ ~map[], comparable, any]( ) [] { := make([], 0, len())for := range { = append(, ) }return}
The pages are generated with Goldsv0.7.6. (GOOS=linux GOARCH=amd64)