54 lines
895 B
Go
54 lines
895 B
Go
package init
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/samber/do"
|
|
)
|
|
|
|
type ServiceInterface interface {
|
|
QueryData(name string) string
|
|
}
|
|
|
|
type AService struct {
|
|
}
|
|
|
|
func (s *AService) QueryData(name string) string {
|
|
return "aString"
|
|
}
|
|
|
|
func NewAService(i *do.Injector) (ServiceInterface, error) {
|
|
return &AService{}, nil
|
|
}
|
|
|
|
type BService struct {
|
|
}
|
|
|
|
func (s *BService) QueryData(name string) string {
|
|
return "bString"
|
|
}
|
|
|
|
func NewBService(i *do.Injector) (ServiceInterface, error) {
|
|
return &BService{}, nil
|
|
}
|
|
|
|
func TestDo(t *testing.T) {
|
|
container := do.New()
|
|
// do.Provide(container, NewAService)
|
|
|
|
// aService := do.MustInvoke[ServiceInterface](container)
|
|
aService, err := do.Invoke[ServiceInterface](container)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
sReturn := aService.QueryData("name")
|
|
if sReturn == "aString" {
|
|
t.Logf("注入a服务")
|
|
}
|
|
|
|
if sReturn == "bString" {
|
|
t.Logf("注入b服务")
|
|
}
|
|
}
|