Go 公式のモジュール照会 API を試す

公開された Go パッケージやモジュールのメタデータを照会する API が公開されたそうだ。

Built for stability and efficient caching, the API uses a stateless, GET-only architecture. Primary endpoints are currently hosted under the /v1beta path. Following a period of community feedback and confirmed stability, we intend to transition toward a formal v1 release.

というわけで現時点では v1beta として提供されている。 エンドポイントは以下の通り:

Endpoint Description
/v1beta/package/{path} Information about the package at {path}.
/v1beta/module/{path} Information about the module at {path}.
/v1beta/versions/{path} Versions of the module at {path}.
/v1beta/packages/{path} Information about packages of the module at {path}.
/v1beta/search?q={query} Search results for a given query.
/v1beta/symbols/{path} List of symbols declared by the package at {path}.
/v1beta/imported-by/{path} Paths of packages importing the package at {path}.
/v1beta/vulns/{path} Vulnerabilities of the module or package at {path}.

たとえば標準の errors パッケージの情報を取得するには,以下の URL を指定すればいい。

$ curl -s https://pkg.go.dev/v1beta/package/errors | jq .
{
  "modulePath": "std",
  "version": "v1.26.3",
  "isLatest": true,
  "isStandardLibrary": true,
  "goos": "all",
  "goarch": "all",
  "path": "errors",
  "name": "errors",
  "synopsis": "Package errors implements functions to manipulate errors.",
  "isRedistributable": true
}

もちろん標準以外の公開パッケージも取得できる。

$ curl -s https://pkg.go.dev/v1beta/package/github.com/goark/errs | jq .
{
  "modulePath": "github.com/goark/errs",
  "version": "v1.3.4",
  "isLatest": true,
  "isStandardLibrary": false,
  "goos": "all",
  "goarch": "all",
  "path": "github.com/goark/errs",
  "name": "errs",
  "synopsis": "Package errs implements functions to manipulate error instances.",
  "isRedistributable": true
}

モジュールの情報も取れる。

$ curl -s https://pkg.go.dev/v1beta/module/github.com/goark/errs | jq .
{
  "path": "github.com/goark/errs",
  "version": "v1.3.4",
  "commitTime": "2026-05-09T04:07:47Z",
  "isLatest": true,
  "isRedistributable": true,
  "isStandardLibrary": false,
  "hasGoMod": true,
  "repoUrl": "https://github.com/goark/errs"
}

面白いところでは脆弱性情報も取得できるようだ。 たとえば昨年の golang.org/x/crypto v0.45.0 における脆弱性情報であれば

$ curl -s https://pkg.go.dev/v1beta/vulns/golang.org/x/crypto?version=v0.45.0 | jq .
{
  "items": [
    {
      "id": "GO-2026-5005",
      "summary": "",
      "details": "Invoking key constraints not enforced in golang.org/x/crypto/ssh/agent",
      "fixedVersion": ""
    },
    {
      "id": "GO-2026-5006",
      "summary": "",
      "details": "Invoking agent constraints dropped when forwarding keys in golang.org/x/crypto/ssh/agent",
      "fixedVersion": ""
    },

    ...

    {
      "id": "GO-2026-5033",
      "summary": "",
      "details": "Invoking pathological inputs can lead to client panic in golang.org/x/crypto/ssh/agent",
      "fixedVersion": ""
    }
  ],
  "total": 13
}

という感じに取得できる(数が多いので途中を端折ってる。あしからず)。 CVE ID ベースじゃないんだな。

CLI ツールも提供されているようだ。 Go のビルド環境があれば以下のコマンドでインストールできる。

$ go install golang.org/x/pkgsite/cmd/internal/pkgsite-cli@latest

ただ,パッケージのパスを見れば分かるように,あくまで API 利用のリファレンス実装という位置づけっぽい。

自作ツールで依存関係を可視化する goark/depm があるのだが,こいつは内部で go コマンドを呼んでいる。 これを API ベースに書き換えたら面白いかな。 いや,ますます遅くなりそうだな。 ちょっと考えるところだ。

参考図書

photo
プログラミング言語Go (ADDISON-WESLEY PROFESSIONAL COMPUTING SERIES)
Alan A.A. Donovan (著), Brian W. Kernighan (著), 柴田 芳樹 (翻訳)
丸善出版 2016-06-20
単行本(ソフトカバー)
4621300253 (ASIN), 9784621300251 (EAN), 4621300253 (ISBN)
評価     

著者のひとりは(あの「バイブル」とも呼ばれる)通称 “K&R” の K のほうである。この本は Go 言語の教科書と言ってもいいだろう。と思ったら絶版状態らしい(2025-01 現在)。復刊を望む!

reviewed by Spiegel on 2016-07-13 (powered by PA-APIv5)

photo
効率的なGo ―データ指向によるGoアプリケーションの性能最適化
Bartłomiej Płotka (著), 山口 能迪 (翻訳)
オライリー・ジャパン 2024-02-24
単行本(ソフトカバー)
4814400535 (ASIN), 9784814400539 (EAN), 4814400535 (ISBN)
評価     

版元で Ebook を買える。Go言語のリファレンス本ではない。フトウェア工学,プログラミング(の考え方)を学ぶ教科書的な位置づけかなぁ。

reviewed by Spiegel on 2024-04-21 (powered by PA-APIv5)

photo
Go言語で学ぶ並行プログラミング 他言語にも適用できる原則とベストプラクティス impress top gearシリーズ
James Cutajar (著), 柴田 芳樹 (著)
インプレス 2024-12-04 (Release 2024-12-04)
Kindle版
B0DNYMMBBQ (ASIN)
評価     

読書会のために購入。インプレス社の本は Kindle 版より版元で PDF 版を買うのがオススメ。「並行処理」について原理的な解説から丁寧に書かれている。 Go で解説されているが Go 以外の言語でも応用できる。

reviewed by Spiegel on 2025-01-25 (powered by PA-APIv5)