See Go IoC in Action
Familiar Spring-style syntax meets Go's compile-time safety
- 1. Define Services
- 2. Create Components
- 3. Generate Wire Code
- 4. Use It
// Define your service interface
type UserService interface {
CreateUser(name string) (*User, error)
GetUser(id string) (*User, error)
}
// Create IoC components with familiar syntax
type UserServiceImpl struct {
Component struct{} // @Component
Implements struct{} `implements:"UserService"` // @Service
Qualifier struct{} `value:"primary"` // @Qualifier
// Dependency injection with @Autowired
Database DatabaseInterface `autowired:"true"`
Logger LoggerInterface `autowired:"true" qualifier:"console"`
Cache CacheInterface `autowired:"true" qualifier:"redis"`
}
func (s *UserServiceImpl) CreateUser(name string) (*User, error) {
s.Logger.Info("Creating user: " + name)
// Implementation here...
return &User{Name: name}, nil
}
// Generated wire code (wire/wire_gen.go)
type Container struct {
DatabaseService *database.DatabaseService
LoggerService *logger.ConsoleLogger
CacheService *cache.RedisCache
UserService *user.UserServiceImpl
}
func Initialize() (*Container, func()) {
container := &Container{}
// Dependencies resolved in correct order
container.DatabaseService = database.NewDatabaseService()
container.LoggerService = &logger.ConsoleLogger{}
container.CacheService = cache.NewRedisCache()
// Inject dependencies automatically
container.UserService = &user.UserServiceImpl{
Database: container.DatabaseService,
Logger: container.LoggerService,
Cache: container.CacheService,
}
return container, cleanup
}
// Use in your application
func main() {
// One line initialization
container, cleanup := wire.Initialize()
defer cleanup()
// Get your services ready to use
userService := container.UserService
// All dependencies are injected and ready!
user, err := userService.CreateUser("John Doe")
if err != nil {
log.Fatal(err)
}
fmt.Printf("Created user: %s\n", user.Name)
}
🏷️ Familiar Annotations
Spring-like @Component, @Autowired, and @Qualifier syntax
⚡ Zero Runtime Cost
Pure compile-time code generation with no reflection or runtime overhead
🔒 Type Safe
All dependencies validated at compile time with full Go type safety
⚠️ Important: Go Anti-Patterns & Migration Strategy
This library intentionally violates Go idioms to provide a familiar migration bridge for Java/Spring teams transitioning to Go. While technically functional, it introduces anti-patterns like magic struct tags and global state containers.
Use Case: Temporary productivity bridge for Java developers learning Go. The compile-time approach ensures clean migration to idiomatic Go patterns when ready.
Spring-like Syntax
Familiar @Autowired and @Component syntax for Java developers. Use struct tags and markers for clean, Spring-style dependency injection in Go.
Compile-time Safety
Zero runtime overhead with pure code generation. All dependencies are resolved at compile time, ensuring type safety and optimal performance.
Advanced Analysis
Built-in dependency graph visualization, circular dependency detection, and comprehensive component analysis tools for better architecture insights.
Get Started in Minutes
Install Go IoC and start using dependency injection in your Go projects
1. Install the CLI
go install github.com/tuhuynh27/go-ioc/cmd/iocgen@latest
Add Components
Mark your structs with IoC annotations using struct tags
Generate Code
Run iocgen to generate type-safe wire code
Use Container
Initialize and use your dependency-injected services