Skip to content
/ wool Public

🐑 a runtime golang dependency injection (di) framework. / golang的运行时依赖注入框架

Notifications You must be signed in to change notification settings

AImager/wool

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

wool

Install

go get github.com/AImager/wool

Usage

package main

import (
	"fmt"
	"github.com/AImager/wool"
)

type AI interface {
	testA()
}

type A1 struct{}

type A2 struct{}

func (a1 *A1) testA() {
	fmt.Println("a1")
}

func (a2 *A2) testA() {
	fmt.Println("a2")
}

func main() {
	// bind interface with factory method
	aFunc := func(name string) AI {
		switch name {
		case "A1":
			return &A1{}
		case "A2":
			return &A2{}
		default:
			return *new(AI)
		}
	}
	_ = wool.DI().Bind(new(AI), aFunc)

	var ai = (AI)(nil)
	_ = wool.DI().Make(&ai, "A2")

	ai.testA()

	// bind interface with factory
	// golang can't find implement struct now
	//_ = di.DI().Bind(new(AI), A1{})
	//
	//ai1 := *new(AI)
	//_ = di.DI().Make(&ai1)
}

Contributing

PRs accepted.

License

MIT © AImager

About

🐑 a runtime golang dependency injection (di) framework. / golang的运行时依赖注入框架

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages