service

package module
v1.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 4, 2021 License: MIT Imports: 4 Imported by: 13

README

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.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Extend added in v1.2.0

func Extend(name string, f ExtenderFunc)

Extend service

func Fill added in v1.2.0

func Fill(name string, dst interface{})

Fill dst

func Get added in v1.2.0

func Get(name string) interface{}

Get service

func GetKeys added in v1.2.0

func GetKeys() []string

GetKeys of all services

func Has added in v1.2.0

func Has(name string) bool

Has service exists

func Set added in v1.2.0

func Set(name string, f ContainerFunc)

Set service

func SetValue added in v1.4.0

func SetValue(name string, v interface{})

SetValue static

Types

type Container

type Container interface {
	// SetValue static
	SetValue(name string, v interface{})
	// Set service
	Set(name string, f ContainerFunc)
	// Has service exists
	Has(name string) bool
	// Get service
	Get(name string) interface{}
	// GetKeys of all services
	GetKeys() []string
	// Fill dst
	Fill(name string, dst interface{})
	// Extend service
	Extend(name string, f ExtenderFunc)
}

Container interface

func New

func New() Container

New constructor

type ContainerFunc

type ContainerFunc func(container Container) interface{}

ContainerFunc type

type ExtenderFunc added in v1.1.0

type ExtenderFunc interface{}

ExtenderFunc type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL