Popularity
5.3
Growing
Activity
5.1
-
225
10
13

Programming language: Go
License: MIT License
Latest version: v1.9.0

di alternatives and similar packages

Based on the "Dependency Injection" category.
Alternatively, view di alternatives based on common mentions on social networks and blogs.

Do you think we are missing an alternative of di or a related project?

Add another 'Dependency Injection' Package

README

DI

Documentation Release Build Status Go Report Card Code Coverage

Dependency injection for Go programming language.

[Tutorial](./docs/tutorial.md) | [Examples](./_examples) | [Advanced features](./docs/advanced.md)

Dependency injection is one form of the broader technique of inversion of control. It is used to increase modularity of the program and make it extensible.

This library helps you to organize responsibilities in your codebase and make it easy to combine low-level implementation into high-level behavior without boilerplate.

Features

  • Intuitive auto wiring
  • Interface implementations
  • Constructor injection
  • Optional injection
  • Field injection
  • Lazy-loading
  • Tagging
  • Grouping
  • Cleanup

Documentation

You can use standard pkg.go.dev and inline code comments. If you do not have experience with auto-wiring libraries as google/wire, uber-go/dig or another - start with [tutorial](./docs/tutorial.md).

Install

go get github.com/goava/di

What it looks like

package main

import (
    "context"
    "fmt"
    "log"
    "net/http"
    "os"
    "os/signal"
    "syscall"

    "github.com/goava/di"
)

func main() {
    di.SetTracer(&di.StdTracer{})
    // create container
    c, err := di.New(
        di.Provide(NewContext),  // provide application context
        di.Provide(NewServer),   // provide http server
        di.Provide(NewServeMux), // provide http serve mux
        // controllers as []Controller group
        di.Provide(NewOrderController, di.As(new(Controller))),
        di.Provide(NewUserController, di.As(new(Controller))),
    )
    // handle container errors
    if err != nil {
        log.Fatal(err)
    }
    // invoke function
    if err := c.Invoke(StartServer); err != nil {
        log.Fatal(err)
    }
}

Full code available [here](./_examples/tutorial/main.go).

Questions

If you have any questions, feel free to create an issue.