Documentation
¶
Overview ¶
Package gioc provides a lightweight, type-safe Inversion of Control (IoC) container for Go applications.
It supports three dependency lifetimes (Singleton, Transient, Scoped), automatic constructor injection, interface binding, and build-time dependency graph validation.
Quick start:
c := gioc.New()
gioc.Provide(c, NewDatabase)
gioc.Provide(c, NewUserRepository)
gioc.Provide(c, NewUserService)
if err := c.Validate(); err != nil {
log.Fatal(err)
}
svc := gioc.Resolve[*UserService](c)
For convenience, a default global container is available:
gioc.Register(NewDatabase) db := gioc.Get[*Database]()
Index ¶
- func BeginScope(ctx context.Context) context.Context
- func Get[T any]() T
- func GetFrom[T any](ctx context.Context) T
- func Has[T any](c *Container) bool
- func Provide(c *Container, constructor any, opts ...Option)
- func ProvideAs[I any](c *Container, constructor any, opts ...Option)
- func ProvideInstance[T any](c *Container, instance T)
- func Register(constructor any, opts ...Option)
- func RegisterAs[I any](constructor any, opts ...Option)
- func RegisterInstance[T any](instance T)
- func Reset()
- func Resolve[T any](c *Container) T
- func ResolveFrom[T any](c *Container, ctx context.Context) T
- func Validate() error
- type Container
- type Option
- type Scope
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BeginScope ¶ added in v1.1.0
BeginScope creates a scoped context using the default container.
Example:
func handler(w http.ResponseWriter, r *http.Request) {
ctx := gioc.BeginScope(r.Context())
svc := gioc.GetFrom[*RequestService](ctx)
}
func Get ¶ added in v1.2.0
func Get[T any]() T
Get retrieves an instance of type T from the default container.
Example:
db := gioc.Get[*Database]()
func GetFrom ¶ added in v1.2.0
GetFrom retrieves an instance of type T from the default container, using the given context for scoped resolution.
Example:
ctx := gioc.BeginScope(r.Context()) svc := gioc.GetFrom[*RequestService](ctx)
func Provide ¶ added in v1.2.0
Provide registers a constructor function with the container. The constructor's return type becomes the registration key. Constructor parameters are automatically resolved from the container.
Supported signatures: func() T, func(D1) T, func(D1, D2) T, ...
Example:
gioc.Provide(c, NewDatabase) gioc.Provide(c, NewCache, gioc.AsTransient())
func ProvideAs ¶ added in v1.2.0
ProvideAs registers a constructor and binds it to the interface type I.
Example:
gioc.ProvideAs[Logger](c, NewConsoleLogger) logger := gioc.Resolve[Logger](c) // returns *ConsoleLogger as Logger
func ProvideInstance ¶ added in v1.2.0
ProvideInstance registers a pre-created value as a singleton. The type parameter T determines the registration key, so you can register concrete types or interfaces:
gioc.ProvideInstance[*Database](c, db) // register as *Database gioc.ProvideInstance[Logger](c, consoleLog) // register as Logger interface
func Register ¶ added in v1.2.0
Register registers a constructor with the default container.
Example:
gioc.Register(NewDatabase) gioc.Register(NewCache, gioc.AsTransient())
func RegisterAs ¶ added in v1.2.0
RegisterAs registers a constructor bound to interface type I with the default container.
Example:
gioc.RegisterAs[Logger](NewConsoleLogger)
func RegisterInstance ¶ added in v1.1.0
func RegisterInstance[T any](instance T)
RegisterInstance registers a pre-created instance with the default container.
Example:
gioc.RegisterInstance[*Database](db) gioc.RegisterInstance[Logger](consoleLogger)
func Reset ¶ added in v1.2.0
func Reset()
Reset clears all bindings from the default container. Primarily useful for testing.
func Resolve ¶ added in v1.2.0
Resolve retrieves an instance of type T from the container.
Example:
db := gioc.Resolve[*Database](c)
func ResolveFrom ¶ added in v1.2.0
ResolveFrom retrieves an instance of type T, using the given context for scoped dependency resolution.
Example:
ctx := c.BeginScope(r.Context()) svc := gioc.ResolveFrom[*RequestService](c, ctx)
Types ¶
type Container ¶ added in v1.2.0
type Container struct {
// contains filtered or unexported fields
}
Container manages dependency bindings and their resolution.
func Default ¶ added in v1.2.0
func Default() *Container
Default returns the global default container.
func (*Container) BeginScope ¶ added in v1.2.0
BeginScope returns a child context that carries a new scope. Scoped dependencies resolved with this context share instances within the same scope.
Example:
func handler(w http.ResponseWriter, r *http.Request) {
ctx := container.BeginScope(r.Context())
svc := gioc.ResolveFrom[*RequestService](container, ctx)
}
func (*Container) Clear ¶ added in v1.2.0
func (c *Container) Clear()
Clear removes all bindings from the container.
type Option ¶ added in v1.2.0
type Option func(*bindingConfig)
Option configures how a dependency is registered.
func AsScoped ¶ added in v1.2.0
func AsScoped() Option
AsScoped marks the binding as scoped (one instance per scope context).
func AsSingleton ¶ added in v1.2.0
func AsSingleton() Option
AsSingleton marks the binding as singleton (default).
func AsTransient ¶ added in v1.2.0
func AsTransient() Option
AsTransient marks the binding as transient (new instance every time).
Directories
¶
| Path | Synopsis |
|---|---|
|
examples
|
|
|
basic
command
|
|
|
constructor_injection
command
|
|
|
cycle_detection
command
|
|
|
dependency_injection
command
|
|
|
interface_based
command
|
|
|
scope_example
command
|