-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathstd_libs.go
47 lines (42 loc) · 901 Bytes
/
std_libs.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 dragon
import (
"bufio"
"go/build"
"os"
"path/filepath"
"regexp"
"strings"
"golang.org/x/sync/errgroup"
)
var sym = regexp.MustCompile(`^pkg (\S+).*?, (?:var|func|type|const) ([A-Z]\w*)`)
func stdLibs(libChan chan lib) error {
apiFiles, err := filepath.Glob(filepath.Join(build.Default.GOROOT, "api", "go1.*txt"))
if err != nil {
return err
}
eg := &errgroup.Group{}
for _, f := range apiFiles {
r, err := os.Open(f)
if err != nil {
return err
}
eg.Go(func() error {
sc := bufio.NewScanner(r)
for sc.Scan() {
l := sc.Text()
has := func(v string) bool { return strings.Contains(l, v) }
if has("struct, ") || has("interface, ") || has(", method (") {
continue
}
if m := sym.FindStringSubmatch(l); m != nil {
libChan <- lib{
object: m[2],
path: m[1],
}
}
}
return sc.Err()
})
}
return eg.Wait()
}