Vessel
Type-safe dependency injection container for Go
Vessel is a standalone, type-safe dependency injection container for Go. It provides constructor-based injection, service lifecycle management, flexible resolution strategies, and support for lazy and optional dependencies. Vessel is the DI engine that powers Forge, but it can be used independently in any Go project.
Installation
go get github.com/xraph/vesselRecommended Pattern
Use constructor injection by type as the default pattern. Dependencies are resolved automatically from function parameter types.
import "github.com/xraph/vessel"
c := vessel.New()
vessel.ProvideConstructor(c, func() *Database {
return &Database{DSN: "postgres://localhost:5432/app"}
})
vessel.ProvideConstructor(c, func(db *Database) *UserService {
return &UserService{db: db}
})
// Resolve by type -- no string keys needed
userService, err := vessel.InjectType[*UserService](c)This keeps your dependency graph explicit, avoids string-key drift, and provides compile-time type safety.
Key Features
- Type-safe generics -- compile-time type checking via Go generics
- Constructor injection -- Uber dig-style automatic dependency resolution with
In/Outstructs - Multiple lifecycles -- singleton, transient, and scoped services
- Typed service keys --
ServiceKey[T]for compile-time safe registration and resolution - Lazy and optional dependencies --
Lazy[T],OptionalLazy[T],Provider[T] - Service lifecycle -- built-in
Start/Stop/Healthmanagement with topological ordering - Circular dependency detection -- detected at resolution time with clear error paths
- Concurrency safe -- thread-safe container operations
- Middleware hooks -- intercept resolve and lifecycle events
- Service discovery -- query and filter services by lifecycle, group, or metadata
- Batch registration -- register multiple services in a single call
Documentation
Quick Start
Create a container, register services, and resolve dependencies in minutes.
Constructors
Constructor injection with In/Out structs, named instances, groups, and options.
Typed Injection
Register services with explicit typed dependency declarations using Provide.
Lifetimes and Scopes
Singleton, transient, and scoped service lifetimes with request-scope patterns.
Lazy and Optional
Defer resolution with Lazy, handle missing services with OptionalLazy, and use Provider for transients.
Typed Keys
Compile-time safe service keys with ServiceKey for registration and resolution.
Service Discovery
Query services by lifecycle, group, or metadata. Batch registration patterns.
Middleware
Intercept resolve and lifecycle events for logging, metrics, and validation.
Forge Integration
How Forge exposes Vessel for app services and extension wiring.
How is this guide?