Documentation
¶
Overview ¶
Package gopkg provides a small client for searching Go packages indexed by pkg.go.dev.
The package is intentionally minimal: you can use Search for quick lookups or configure a Client when you need a custom HTTP client or a pkg.go.dev-compatible base URL for testing.
Results are returned as import paths, which makes the package useful for tooling, scripts, CLIs, and editor integrations.
Index ¶
Examples ¶
Constants ¶
const DefaultBaseURL = "https://pkg.go.dev"
DefaultBaseURL is the default host queried by Client.
Variables ¶
var ErrEmptyQuery = errors.New("query must not be empty")
ErrEmptyQuery is returned when a search query is blank or only whitespace.
Functions ¶
func FormatResults ¶
FormatResults joins import paths into newline-delimited output. When limit is greater than zero, only the first limit entries are included.
Example ¶
package main
import (
"fmt"
"github.com/alex-held/gopkg"
)
func main() {
results := []string{
"github.com/spf13/cobra",
"github.com/spf13/pflag",
}
fmt.Println(gopkg.FormatResults(results, 1))
}
Output: github.com/spf13/cobra
Types ¶
type Client ¶
Client searches the pkg.go.dev index.
Zero values are usable: a nil HTTP client falls back to http.DefaultClient, and an empty BaseURL falls back to DefaultBaseURL.
func (*Client) Search ¶
Search queries the configured pkg.go.dev-compatible endpoint and returns matching import paths in the same order they appear in the search results page.
Example ¶
package main
import (
"context"
"fmt"
"net/http"
"net/http/httptest"
"github.com/alex-held/gopkg"
)
func main() {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write([]byte(`
<div class="SearchSnippet-headerContainer"><a href="/github.com/spf13/cobra">cobra</a></div>
<div class="SearchSnippet-headerContainer"><a href="/github.com/spf13/pflag">pflag</a></div>
`))
}))
defer server.Close()
client := gopkg.NewClient()
client.BaseURL = server.URL
results, err := client.Search(context.Background(), "cobra")
if err != nil {
fmt.Println("error:", err)
return
}
fmt.Println(gopkg.FormatResults(results, -1))
}
Output: github.com/spf13/cobra github.com/spf13/pflag