Skip to content

tzvatot/injector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 

Repository files navigation

injector

Dependency Injection engine for Go

Usage

Assume you have the following interface:

package injector

type Incrementor interface {
	Inc(int) int
}

This interface have an implementation as well:

package injector

type MyImplementation struct {
}

func (m *MyImplementation) Inc(x int) int {
	return x + 1
}

And there's a struct that would like to use that interface, and inject it with a certain implementation:

package injector

type MyStruct struct {
	MyIncrementor Incrementor `inject:"injector.MyImplementation"`
}

The following example can invoke the injection:

incrementor := &MyImplementation{}
toInject := &MyStruct{}
if err := injector.Register(incrementor, toInject); err != nil {
	panic(fmt.Sprintf("failed to register: %v", err))
}
if err := injector.Inject(); err != nil {
	panic(fmt.Sprintf("failed to inject: %v", err))
}

result := toInject.MyIncrementor.Inc(5)
fmt.Println(result) // 6

Notes

  • Injected field must be exported in order for assignment to work.
  • Injection of struct pointer is also supported without tags.

About

Dependency Injection engine for Go

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages