Skip to content

euskadi31/go-service

Repository files navigation

Go Service Container Last release Documentation

Go Report Card

Branch Status Coverage
master Build Status Coveralls

Simple Dependency Injection Container for Golang

Example

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())
}

License

go-service is licensed under the MIT license.