An open API service indexing awesome lists of open source software.

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

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 main

import "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() + msg

s.simpleClient.SendText(to, msg, "[email protected]")
}

```