Skip to content

michaelestrin/go-mod-di

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-mod-di

Simple dependency injection container originally implemented by michaelestrin for edgexfoundry/go-mod-bootstrap in December 2019 and extracted here in June 2023 for general reuse.

Requires Go 1.18 or later.

Usage

	package main

	import (
		"fmt"
		"github.com/michaelestrin/go-mod-di/pkg/di"
	)

	type foo struct {
		FooMessage string
	}

	func NewFoo(m string) *foo {
		return &foo{
			FooMessage: m,
		}
	}

	type bar struct {
		BarMessage string
		Foo        *foo
	}

	func NewBar(m string, foo *foo) *bar {
		return &bar{
			BarMessage: m,
			Foo:        foo,
		}
	}

	func main() {
		container := di.NewContainer(
			di.ServiceConstructorMap{
				"foo": func(get di.Get) interface{} {
					return NewFoo("fooMessage")
				},
				"bar": func(get di.Get) interface{} {
					return NewBar("barMessage", get("foo").(*foo))
				},
			})

		b := container.Get("bar").(*bar)
		fmt.Println(b.BarMessage)
		fmt.Println(b.Foo.FooMessage)
	}

Background

Dependency Injection in EdgeX Core Services

About

Simple dependency injection container

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages