Get Started in 5 Minutes
godi is a dependency injection library that automatically wires up your Go applications. Define your services, specify their lifetimes, and let godi handle the rest.
What you’ll learn:
Create a container - Where your services live
Register services - Tell godi about your types
Resolve dependencies - Let godi wire everything together
Use lifetimes - Control when instances are created
Add HTTP integration - Build web applications
Before You Start
You need:
Go 1.21 or later
A text editor
5 minutes
Tutorial Overview
Page |
Time |
What You’ll Build |
|---|---|---|
30 sec |
Install godi |
|
60 sec |
Create and use a container |
|
90 sec |
Wire up real services |
|
90 sec |
Control instance creation |
|
90 sec |
Build a web server |
|
30 sec |
Where to go from here |
Quick Preview
Here’s what a complete godi application looks like:
package main
import (
"fmt"
"github.com/junioryono/godi/v4"
)
// Your services - normal Go types
type Logger struct{}
func (l *Logger) Log(msg string) { fmt.Println(msg) }
type UserService struct {
logger *Logger
}
func NewUserService(logger *Logger) *UserService {
return &UserService{logger: logger}
}
func main() {
// Register services
services := godi.NewCollection()
services.AddSingleton(func() *Logger { return &Logger{} })
services.AddSingleton(NewUserService)
// Build and use
provider, _ := services.Build()
defer provider.Close()
// godi automatically wires Logger into UserService
users := godi.MustResolve[*UserService](provider)
users.logger.Log("Hello, godi!")
}
Ready? Let’s start with installation.