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.
Installation01
go get github.com/xraph/vesselRecommended Pattern02
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 Features03
Type-safe generics -- compile-time type checking via Go generics
Constructor injection -- automatic dependency resolution with
In/OutstructsMultiple lifecycles -- singleton, transient, and scoped services
Named instances --
WithNameandWithAliasesfor multiple instances of the same typeLazy and optional dependencies --
Lazy[T],OptionalLazy[T],Provider[T]Service lifecycle -- built-in
Start/Stop/Healthmanagement with topological orderingCircular 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
Documentation04
Create a container, register services, and resolve dependencies in minutes.
Constructor injection with In/Out structs, named instances, groups, and options.
Singleton, transient, and scoped service lifetimes with request-scope patterns.
Defer resolution with Lazy, handle missing services with OptionalLazy, and use Provider for transients.
Query services by lifecycle, group, or metadata.
Intercept resolve and lifecycle events for logging, metrics, and validation.
How Forge exposes Vessel for app services and extension wiring.