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.Provide(c, func() *Database {
return &Database{DSN: "postgres://localhost:5432/app"}
})
vessel.Provide(c, func(db *Database) *UserService {
return &UserService{db: db}
})
// Resolve by type -- no string keys needed
userService, err := vessel.Inject[*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 -- automatic dependency resolution with
In/Outstructs - Multiple lifecycles -- singleton, transient, and scoped services
- Named instances --
WithNameandWithAliasesfor multiple instances of the same type - 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
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.
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.
Service Discovery
Query services by lifecycle, group, or metadata.
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?