Branch | Status | Coverage |
---|---|---|
master |
Simple Dependency Injection Container for Golang
package main
import (
"fmt"
"github.com/euskadi31/go-service"
)
type MyService struct {
name string
}
func (s *MyService) SetName(name string) {
s.name = name
}
func (s *MyService) Name() string {
return s.name
}
func main() {
sc := service.New()
// Define service
sc.Set("my.service", func(c service.Container) interface{} {
return &MyService{}
})
// Extend service
sc.Extend("my.service", func(s *MyService) *MyService {
s.SetName("My Service")
return s
})
// Call service
myService := sc.Get("my.service").(*MyService)
fmt.Printf("Service Name: %s", myService.Name())
}
go-service is licensed under the MIT license.