https://github.com/morilog/gioc
Golang inversion of control library IoC
https://github.com/morilog/gioc
Last synced: 3 months ago
JSON representation
Golang inversion of control library IoC
- Host: GitHub
- URL: https://github.com/morilog/gioc
- Owner: morilog
- Created: 2020-06-18T08:31:59.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2020-06-18T09:13:05.000Z (about 5 years ago)
- Last Synced: 2023-08-11T04:53:17.581Z (almost 2 years ago)
- Language: Go
- Size: 2.93 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Golang IoC
This is a pure golang library to using inversion of control (ioc) in golang## Installation
```bash
go get github.com/morilog/gioc
```## Example
```golang
package mainimport "guthub.com/morilog/gioc"
import "log"
import "fmt"func main() {
// Bind binds a resolver to abstract type and
// the resolver called every time you needed to the type
gioc.Bind(func () (Greeter, err) {
return &GoodMorning{}
})var g Greeter
if err := gioc.Make(&g); err != nil {
log.Fatal(err)
}fmt.Println(g.SayHello()) // prints "good morning"
gioc.Singleton(func (greeter Greeter, c *SimpleClient) (Mailer, error)) {
return &simpleMailer(simpleClient: c, g: greeter), nil
}var m Mailer
if err := gioc.Make(&m); err != nil {
log.Fatal(err)
}m.Send("[email protected]", "don't reply me")
}type Greeter interface{
SayHello() string
}type Morning struct{}
func (g Morning) SayHello() string {
return "good morning"
}type Mailer interface {
Send(to string, msg string) error
}type SimpleMailer struct{
g Greeter
simpleClient *client
}func (s *SimpleMailer) Send(to string, msg string) error {
msg = s.g.SayHello() + msgs.simpleClient.SendText(to, msg, "[email protected]")
}```