Skip to content

Simple dependency injection service container in golang

License

Notifications You must be signed in to change notification settings

ss-limafriyadi/gocontainer

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go-container

GoDoc Go Report Card

Simple dependency injection service container for golang

Usage

package main

import (
	"fmt"
	"github.com/ncrypthic/gocontainer"
)

type Seed struct {
	Name string
}
type BaseStruct struct {
	Id string `inject:"seed"`
}
type CoolStruct struct {
	Name string
	Base BaseStruct `inject:"base"`
}

func main() {
	container := gocontainer.NewContainer()
	container.RegisterService("cool", &CoolStruct{Name: "a cool struct"})
	container.RegisterService("seed", &Seed{"the seed"})
	container.RegisterService("base", &BaseStruct{"base deps"})
	container.Ready() // Populate dependency tree
	seed, _ := container.GetService("seed")
	base, _ := container.GetService("base")
	cool, _ := container.GetService("cool")
	fmt.Println(seed.(*Seed).Name)
	fmt.Println(base.(*BaseStruct).Id)
	fmt.Println(cool.(*CoolStruct).Name)
	// Output:
	// the seed
	// base deps
	// a cool struct
}

License

MIT License

About

Simple dependency injection service container in golang

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%