
Container
An IoC Container for Go projects.
It provides simple, fluent and easy-to-use APIs to make dependency injection in GoLang very easier.
Documentation
Supported Versions
It requires Go v1.11 or newer versions.
Installation
To install this package run following command in the root of your project
go get github.com/golobby/container
Binding
To bind an abstraction to a concrete for further singleton resolutions:
container.Singleton(func() Abstraction {
return Implementation
})
It invokes the resolver function once and always return the same object each time you call make() method.
And to bind an abstraction to a concrete for further transient resolutions:
container.Transient(func() Abstraction {
return Implementation
})
It invokes the resolver function to provide a brand new object each time you call make() method.
Take a look at examples below:
Singleton example:
container.Singleton(func() Database {
return &MySQL{}
})
Transient example:
container.Transient(func() Shape {
return &Rectangle{}
})
Resolving
To make a concrete by its abstraction:
container.Make(func(a Abstraction) {
// a will be a concrete of Abstraction
})
For example:
container.Make(func(db Database) {
// db is an instance of MySQL
db.Query("...")
})
You can resolve multiple abstractions:
container.Make(func(db Database, s Shape) {
// db is an instance of MySQL
// s is an instance of Rectangle
db.Query("...")
s.Area()
})
License
GoLobby Container is initially created by
@miladrahimi and @amirrezaask,
and released under the MIT License.