dix

package module
v2.0.0-beta.12 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 20, 2026 License: MIT Imports: 4 Imported by: 0

README ΒΆ

dix

Go Reference Go Report Card

dix is a lightweight yet powerful dependency injection framework for Go.

Inspired by uber-go/dig, with support for advanced dependency management and namespace isolation.

δΈ­ζ–‡ζ–‡ζ‘£

✨ Features

Feature Description
πŸ”„ Cycle Detection Auto-detect dependency cycles
πŸ“¦ Multiple Injection Support func, struct, map, list
🏷️ Namespace Dependency isolation via map key
🎯 Multi-Output Struct can provide multiple dependencies
πŸͺ† Nested Support Support nested struct injection
πŸ”§ Non-Invasive Zero intrusion to original objects
πŸ›‘οΈ Safe API TryProvide/TryInject won't panic
🌐 Visualization HTTP module for dependency graph

πŸ“¦ Installation

go get github.com/pubgo/dix/v2

πŸš€ Quick Start

package main

import (
    "fmt"
    "github.com/pubgo/dix/v2"
)

type Config struct {
    DSN string
}

type Database struct {
    Config *Config
}

type UserService struct {
    DB *Database
}

func main() {
    // Create container
    di := dix.New()

    // Register Providers
    dix.Provide(di, func() *Config {
        return &Config{DSN: "postgres://localhost/mydb"}
    })

    dix.Provide(di, func(c *Config) *Database {
        return &Database{Config: c}
    })

    dix.Provide(di, func(db *Database) *UserService {
        return &UserService{DB: db}
    })

    // Inject and use
    dix.Inject(di, func(svc *UserService) {
        fmt.Println("DSN:", svc.DB.Config.DSN)
    })
}

πŸ“– Core API

Provide / TryProvide

Register constructor (Provider) to container:

// Standard version - panics on error
dix.Provide(di, func() *Service { return &Service{} })

// Safe version - returns error
err := dix.TryProvide(di, func() *Service { return &Service{} })
if err != nil {
    log.Printf("Registration failed: %v", err)
}
Inject / TryInject

Inject dependencies from container:

// Function injection
dix.Inject(di, func(svc *Service) {
    svc.DoSomething()
})

// Struct injection
type App struct {
    Service *Service
    Config  *Config
}
app := &App{}
dix.Inject(di, app)

// Safe version
err := dix.TryInject(di, func(svc *Service) {
    // ...
})

🎯 Injection Patterns

Struct Injection
type In struct {
    Config   *Config
    Database *Database
}

type Out struct {
    UserSvc  *UserService
    OrderSvc *OrderService
}

// Multiple inputs and outputs
dix.Provide(di, func(in In) Out {
    return Out{
        UserSvc:  &UserService{DB: in.Database},
        OrderSvc: &OrderService{DB: in.Database},
    }
})
Map Injection (Namespace)
// Provide with namespace
dix.Provide(di, func() map[string]*Database {
    return map[string]*Database{
        "master": &Database{DSN: "master-dsn"},
        "slave":  &Database{DSN: "slave-dsn"},
    }
})

// Inject specific namespace
dix.Inject(di, func(dbs map[string]*Database) {
    master := dbs["master"]
    slave := dbs["slave"]
})
List Injection
// Provide same type multiple times
dix.Provide(di, func() []Handler {
    return []Handler{&AuthHandler{}}
})
dix.Provide(di, func() []Handler {
    return []Handler{&LogHandler{}}
})

// Inject all
dix.Inject(di, func(handlers []Handler) {
    // handlers contains AuthHandler and LogHandler
})

🧩 Modules

dixglobal - Global Container

Provides global singleton container for simple applications:

import "github.com/pubgo/dix/v2/dixglobal"

// Use directly without creating container
dixglobal.Provide(func() *Config { return &Config{} })
dixglobal.Inject(func(c *Config) { /* ... */ })
dixcontext - Context Integration

Bind container to context.Context:

import "github.com/pubgo/dix/v2/dixcontext"

// Store in context
ctx := dixcontext.Create(context.Background(), di)

// Retrieve and use
container := dixcontext.Get(ctx)
dixhttp - Dependency Visualization πŸ†•

Web interface for visualizing dependency graph, designed for large projects:

import (
    "github.com/pubgo/dix/v2/dixhttp"
    "github.com/pubgo/dix/v2/dixinternal"
)

server := dixhttp.NewServer((*dixinternal.Dix)(di))
server.ListenAndServe(":8080")

Visit http://localhost:8080 to view dependency graph.

Highlights:

  • πŸ” Fuzzy Search - Quickly locate types or functions
  • πŸ“¦ Package Grouping - Collapsible sidebar browsing
  • πŸ”„ Bidirectional Tracking - Show both dependencies and dependents
  • πŸ“ Depth Control - Limit display levels (1-5 or all)
  • 🎨 Modern UI - Tailwind CSS + Alpine.js

See dixhttp/README.md for details.

πŸ› οΈ Development

# Run tests
task test

# Lint
task lint

# Build
task build

πŸ“š Examples

Example Description
struct-in Struct input injection
struct-out Struct multi-output
func Function injection
map Map/namespace injection
map-nil Map with nil handling
list List injection
list-nil List with nil handling
lazy Lazy injection
cycle Cycle detection example
handler Handler pattern
inject_method Method injection
test-return-error Error handling
http HTTP visualization

πŸ“– Documentation

Document Description
Design Document Architecture and detailed design
Audit Report Project audit, evaluation and comparison
dixhttp README HTTP visualization module documentation

πŸ“„ License

MIT

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func Inject ΒΆ

func Inject[T any](di *Dix, data T, opts ...Option) T

func InjectT ΒΆ

func InjectT[T any](di *Dix, opts ...Option) T

func Provide ΒΆ

func Provide(di *Dix, data any)

func SetLog ΒΆ

func SetLog(log slog.Handler)

func Version ΒΆ

func Version() string

Types ΒΆ

type Dix ΒΆ

type Dix = dixinternal.Dix

func New ΒΆ

func New(opts ...Option) *Dix

type Option ΒΆ

type Option = dixinternal.Option

func WithValuesNull ΒΆ

func WithValuesNull() Option

type Options ΒΆ

type Options = dixinternal.Options

Directories ΒΆ

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL