whyor

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 0 Imported by: 0

README

whyor

Compile-time dependency injection for Go. Successor to Google Wire, with a generics-based API. Generates plain Go; nothing runs at runtime.

//go:build whyor

var DBSet = whyor.Set(NewConfig, NewPG, whyor.Bind[Store, *PG]())

func InitApp(dsn string) (*App, func(), error) {
	panic(whyor.Build(DBSet, NewApp))
}
go run github.com/Medzoner/whyor/cmd/whyor gen ./...    # writes whyor_gen.go
go run github.com/Medzoner/whyor/cmd/whyor check ./...  # exit 1 if stale

API:

  • Build, Set: declare an injector / group providers.
  • Bind[I, T](): provide interface I with concrete T.
  • Value[T](v): provide a literal.
  • Many[T](providers...): provide a []T.
  • AutoBind[T](): use T for any needed interface it implements (must be unambiguous).
  • Closer[T](): call T.Close() in the injector's cleanup.

Providers are functions returning T, (T, func()), (T, error) or (T, func(), error). See examples/basic.

Documentation

Overview

Package whyor is a compile-time dependency injection toolkit.

Injectors are declared in files guarded by the "whyor" build tag:

//go:build whyor

func InitApp(path string) (*App, func(), error) {
	panic(whyor.Build(NewConfig, NewDB, whyor.Bind[Store, *PG]()))
}

`whyor gen` then writes a whyor_gen.go file containing plain Go code. Nothing in this package does any work at runtime.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Build

func Build(opts ...Option) any

Build declares an injector body. It must be the argument of panic in an injector function. It is replaced by generated code.

Types

type Option

type Option any

Option is a provider, a Set, a Bind or a Value accepted by Build and Set. Plain provider functions are accepted too.

func AutoBind

func AutoBind[T any]() Option

AutoBind makes T usable wherever an interface it implements is needed, as long as exactly one registered AutoBind type implements that interface.

func Bind

func Bind[I, T any]() Option

Bind tells the generator that interface I is satisfied by concrete type T.

func Closer

func Closer[T any]() Option

Closer registers T's Close method as its cleanup: the injector must then return a func(). It applies to providers of T that have no cleanup of their own.

func Many

func Many[T any](opts ...Option) Option

Many provides a []T made of the given providers, each returning a value assignable to T. Its elements are not provided as T on their own.

func Set

func Set(opts ...Option) Option

Set groups providers and options so they can be reused.

func Value

func Value[T any](v T) Option

Value provides the given expression as a dependency of type T.

Directories

Path Synopsis
cmd
whyor command
Command whyor generates dependency injection code.
Command whyor generates dependency injection code.
examples
internal
gen
Package gen implements the whyor code generator.
Package gen implements the whyor code generator.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL