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
|
package pkgset
import (
"sync"
"golang.org/x/tools/go/packages"
)
var stdpkgs Set
var stdonce sync.Once
// LoadStd preloads the std package list.
func LoadStd() {
stdonce.Do(func() {
standard, err := packages.Load(&packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedImports | packages.NeedModule,
Tests: true,
}, "std")
if err != nil {
panic(err)
}
stdpkgs = New(standard...)
})
}
// IsStd returns whether *packages.Package is a std package
func IsStd(p *packages.Package) bool {
LoadStd()
return IsStdName(p.ID)
}
// IsStdName returns whether id corresponds to a standard package
func IsStdName(id string) bool {
LoadStd()
_, ok := stdpkgs[id]
return ok
}
// Std returns the standard package set
func Std() Set {
LoadStd()
return stdpkgs.Clone()
}
|