-
Notifications
You must be signed in to change notification settings - Fork 644
/
Copy pathpackages.go
48 lines (44 loc) · 1.15 KB
/
packages.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
48
//go:generate go run gen.go > packages_gen.go
package golang
import (
"fmt"
"log"
"net/http"
"strings"
"github.com/PuerkitoBio/goquery"
)
// StdPackages returns a map of all standard packages for the given version.
func StdPackages(version string) (map[string]struct{}, error) {
if version != "" {
version = "@go" + version
}
res, err := http.Get(fmt.Sprintf("https://pkg.go.dev/std%s", version))
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
return nil, err
}
packages := map[string]struct{}{}
doc.Find("tbody").Each(func(i int, s *goquery.Selection) {
// For each item found, get the title
s.Find("a").Each(func(i int, s *goquery.Selection) {
href, ok := s.Attr("href")
if !ok {
return
}
// extract the package name from the href
// /crypto/internal/[email protected]
if len(strings.Split(href, "@")) > 1 {
packages[strings.Trim(strings.Split(href, "@")[0], "/")] = struct{}{}
}
})
})
return packages, nil
}