Repository navigation
v0.1.0
English | 日本語
The first release of knit, a lightweight compile-time dependency injection code generator for Go, in the style of google/wire (now archived).
Highlights
- Native mode (default): declare injectors with
knit.Build/knit.NewSet/knit.Bindfromgithub.com/withnic/knit, in files tagged//go:build knitinject. Code is generated intoknit_gen.go. No dependency on google/wire. - wire mode (
-mode=wire): a drop-in replacement for thewirecommand. It reads existingwire.gofiles and writes the samewire_gen.go. - Providers returning
(T),(T, error),(T, func())or(T, func(), error), provider sets (nested, across packages), interface bindings and injector parameters. - Cleanup on failure: if a provider fails, the cleanups of everything built so far run in reverse order (LIFO).
- Deterministic output and clear errors that show the full dependency chain.
- Dependency graph: each run also writes
.knit/di-graph.json, describing what every injector depends on.
Install
Requires Go 1.26 or later (only for go install / importing the marker package).
go install github.com/withnic/knit/cmd/knit@v0.1.0 # or: go get -tool github.com/withnic/knit/cmd/knit@v0.1.0
go get github.com/withnic/knit@v0.1.0 # marker package for native modePrebuilt binaries for Linux, macOS and Windows (amd64 / arm64) are attached below. Verify a download with checksums.txt, or check its build provenance with:
gh attestation verify knit_0.1.0_linux_amd64.tar.gz -R withnic/knitNot supported
wire.Struct, wire.Value, wire.InterfaceValue, wire.FieldsOf, and variadic, method or generic providers are intentionally out of scope; each has a simple alternative.
See the README for the quick start, migration from google/wire and the command reference.
日本語
knit の最初のリリースです。knit は、google/wire(アーカイブ済み)と同じスタイルで使える、Go 向けの軽量なコンパイル時 DI コードジェネレータです。
主な機能
- knit モード(デフォルト):
github.com/withnic/knitのknit.Build/knit.NewSet/knit.Bindを使い、//go:build knitinject付きのファイルで Injector を宣言します。コードはknit_gen.goに生成されます。google/wire への依存は不要です。 - wire モード(
-mode=wire):wireコマンドのドロップイン代替です。既存のwire.goを読み、同じwire_gen.goを生成します。 - プロバイダ:
(T)、(T, error)、(T, func())、(T, func(), error)を返す関数に対応します。プロバイダセット(ネストや別パッケージにも対応)、インターフェースのバインド、Injector の引数も使えます。 - 失敗時のクリーンアップ: プロバイダが失敗したときは、それまでに作ったもののクリーンアップを逆順(LIFO)に実行します。
- 決定論的な出力と、依存のつながりをすべて表示する分かりやすいエラー。
- 依存グラフ: 実行のたびに
.knit/di-graph.jsonも出力し、各 Injector が何に依存しているかを記録します。
インストール
Go 1.26 以上が必要です(go install とマーカーパッケージの import の場合のみ)。
go install github.com/withnic/knit/cmd/knit@v0.1.0 # または: go get -tool github.com/withnic/knit/cmd/knit@v0.1.0
go get github.com/withnic/knit@v0.1.0 # knit モード用のマーカーパッケージLinux・macOS・Windows(amd64 / arm64)向けのビルド済みバイナリを下に添付しています。ダウンロードしたファイルは checksums.txt で確認できます。ビルドの証明は次のコマンドで検証できます。
gh attestation verify knit_0.1.0_linux_amd64.tar.gz -R withnic/knitサポートしない機能
wire.Struct、wire.Value、wire.InterfaceValue、wire.FieldsOf、可変長引数・メソッド・ジェネリックのプロバイダは、意図的に対象外にしています。どれも簡単な代わりの書き方があります。
クイックスタート、google/wire からの移行方法、コマンドの説明は README を参照してください。