/
std.go
47 lines (35 loc) · 785 Bytes
/
std.go
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
package pkgset
import (
"sync"
"golang.org/x/tools/go/packages"
)
var stdpkgs Set
var stdonce sync.Once
func loadstd() {
stdonce.Do(func() {
standard, err := packages.Load(&packages.Config{
Mode: packages.NeedName | packages.NeedFiles | packages.NeedCompiledGoFiles | packages.NeedImports,
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()
}