This is The Way to do Dependency injection in Go.
Don't know what DI is? Wikipedia
Source: https://github.com/go-path/di/tree/main/examples/basic
package padawan
import "github.com/go-path/di"
type JediService interface {
FeelTheForce()
}
type PadawanController struct {
Master JediService `inject:""`
}
func (p *PadawanController) Initialize() {
println("[Padawan] Master, I want to learn the ways of the force...")
p.Master.FeelTheForce()
}
func init() {
// register as startup component, injecting dependencies
di.Injected[*PadawanController](di.Startup(100))
}
package jedi
import "github.com/go-path/di"
type yodaServiceImp struct{}
func (s *yodaServiceImp) FeelTheForce() {
println("[Yoda] Patience You Must Have My Young Padawan")
}
func init() {
di.Register(&yodaServiceImp{})
}
package main
import (
"github.com/go-path/di"
_ "di/example/basic/jedi"
_ "di/example/basic/padawan"
)
func main() {
di.Initialize()
}
output
INFO [di] '*jedi.yodaServiceImp' is a candidate for 'padawan.JediService'
[Padawan] Master, I want to learn the ways of the force...
[Yoda] Patience You Must Have My Young Padawan
- Typesafe: Using generics.
- Extensible: And configurable.
- Ergonomic: Easy to understand. Focused on providing a simple yet efficient API.
- Predictable: The programmer has control over dependencies and can define qualifiers.
- Framework Agnostic: It can be used with any library.
To be a lightweight, simple, ergonomic and high-performance DI container that can be easily extended. A foundation for the development of more complex frameworks and architectural structures.
Read our Full Documentation to learn how to use go-path/di.
ARE YOU ALLERGIC?
Some concepts applied in this library may contain gluten and/or be inspired by the implementations of CDI Java and Spring IoC.
And obviously, this implementation uses reflection, which for some uninformed individuals can be detrimental. It is important to mention that we thoroughly sanitize our hands before handling any
Type
orValue
.We will not be held responsible if you become a productive developer after coming into contact with any part of go-path/di.
"Your path you must decide.” — Yoda
All kinds of contributions are welcome!
🐛 Found a bug?
Let me know by creating an issue.
❓ Have a question?
Discussions is a good place to start.
⚙️ Interested in fixing a bug or adding a feature?
Check out the contributing guidelines.
📖 Can we improve our documentation?
Pull requests even for small changes can be helpful. Each page in the docs can be edited by clicking the
"Edit on GitHub" link at the bottom right.
This code is distributed under the terms and conditions of the MIT license.