Skip to content

renke/injector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

injector

injector is a constructor-based dependency injection library for Go.

Some features and remarks:

  • Only supports constructor injection.
  • Injection based on concrete type, interface or slice of interface.
  • No need to use exported struct fields.
  • Injected dependencies are always singletons.
  • Encourages a lot of constructor functions. Probably not idiomatic Go.
  • Contains a few known bugs. Mostly likely has more.
  • Panics when something bad happens.
  • Lacks useful documentation.

Installation

$ go get github.com/renke/injector

Usage

package main

import (
	"fmt"

	"github.com/renke/injector"
)

type Foo struct {
	name string
}

func NewFoo() *Foo {
	return &Foo{
		name: "Foo",
	}
}

type Bar struct {
	name string
}

func NewBar() *Bar {
	return &Bar{
		name: "Bar",
	}
}

type Baz struct {
	foo *Foo
	bar *Bar
}

func NewBaz(foo *Foo, bar *Bar) *Baz {
	return &Baz{
		foo: foo,
		bar: bar,
	}
}

func (baz *Baz) Print() {
	fmt.Println(baz.foo.name, baz.bar.name, "Baz")
}

type App struct {
	Baz *Baz
}

func main() {
	container := injector.NewContainer()

	container.Register(NewFoo)
	container.Register(NewBar)
	container.Register(NewBaz)

	var app App

	container.Resolve(&app)

	app.Baz.Print()
}

See test cases for more "examples".

Feedback

I appreciate any kind of feedback. Just create an issue or drop me a mail. Thanks!

License

See LICENSE.

About

injector is a constructor-based dependency injection library for Go.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages