func (p *Pkg) String() string {
	b := bytes.NewBufferString(p.Name)
	if !p.Resolved {
		b.Write([]byte(" (unresolved)"))
	}
	return b.String()
}
String returns a string representation of the Pkg containing the Pkg name and status.
package depth
import (
	"bytes"
	"go/build"
	"path"
	"sort"
	"strings"
)
type Pkg struct {
	Name   string `json:"name"`
	SrcDir string `json:"-"`
	Internal bool `json:"-"`
	Resolved bool `json:"resolved"`
	Tree   *Tree `json:"-"`
	Parent *Pkg  `json:"-"`
	Deps   []Pkg `json:"deps"`
	Raw *build.Package `json:"-"`
}
func (p *Pkg) Resolve(i Importer) {
	p.Resolved = true
	name := p.cleanName()
	if name == "" {
		return
	}
	var importMode build.ImportMode
	if p.Tree.hasSeenImport(name) || p.Tree.isAtMaxDepth(p) {
		importMode = build.FindOnly
	}
	pkg, err := i.Import(name, p.SrcDir, importMode)
	if err != nil {
		p.Resolved = false
		return
	}
	p.Raw = pkg
	p.Name = pkg.ImportPath
	if pkg.Goroot {
		p.Internal = true
		if !p.Tree.shouldResolveInternal(p) {
			return
		}
	}
	imports := pkg.Imports
	if p.Tree.ResolveTest {
		imports = append(imports, append(pkg.TestImports, pkg.XTestImports...)...)
	}
	p.setDeps(i, imports, pkg.Dir)
}
func (p *Pkg) setDeps(i Importer, imports []string, srcDir string) {
	unique := make(map[string]struct{})
	for _, imp := range imports {
		if imp == p.Name {
			continue
		}
		if _, ok := unique[imp]; ok {
			continue
		}
		unique[imp] = struct{}{}
		p.addDep(i, imp, srcDir)
	}
	sort.Sort(byInternalAndName(p.Deps))
}
func (p *Pkg) addDep(i Importer, name string, srcDir string) {
	dep := Pkg{
		Name:   name,
		SrcDir: srcDir,
		Tree:   p.Tree,
		Parent: p,
	}
	dep.Resolve(i)
	p.Deps = append(p.Deps, dep)
}
func (p *Pkg) isParent(name string) bool {
	if p.Parent == nil {
		return false
	}
	if p.Parent.Name == name {
		return true
	}
	return p.Parent.isParent(name)
}
func (p *Pkg) depth() int {
	if p.Parent == nil {
		return 0
	}
	return p.Parent.depth() + 1
}
func (p *Pkg) cleanName() string {
	name := p.Name
	if name == "C" {
		return ""
	}
	if strings.HasPrefix(name, "golang_org") {
		name = path.Join("vendor", name)
	}
	return name
}