golang使用wire依赖注入时,解决注入时类型相同时生成wire_gen.go文件时报错:ProviderSet has multiple bindings for xx/xx/xxx.go

问题:类似以下这种创建实例的方式,在ProviderSet中添加各个需要注入的实例后,由于injector的函数中,不允许出现重复的参数类型,否则wire将无法区分这些相同的参数类型,因此编译时会报错。

func NewCertificateUseCase(repo CertificateRepo, huawei HuaweiCertificateRepo,
	ios CertificateRepo,
	iosP8 CertificateRepo,
	meizu CertificateRepo,
	miAndGoogle CertificateRepo,
	oppo CertificateRepo,
	vivo CertificateRepo,
	logger log.Logger) *CertificateUseCase {
	return &CertificateUseCase{repo,
		huawei,
		ios,
		iosP8,
		meizu,
		miAndGoogle,
		oppo,
		vivo,
		log.NewHelper(logger)}
}
var ProviderSet = wire.NewSet(NewData, NewAppRepo, NewImUserStaticRepo, NewUserRepo,
	NewParentCertificateRepo, NewIosP8Certificate, NewIosPCertificate, NewAndHuaweiCertificate,
	NewAndMeizuCertificate, NewMiAndGoogle, NewAndOppoCertificate, NewAndVivoCertificate, NewClientAppRepo)

解决方案:通过创建新的类型来避免此问题。如下:

type HuaweiCertificateRepo CertificateRepo
type IosCertificateRepo CertificateRepo
type IosP8CertificateRepo CertificateRepo
type MeizuCertificateRepo CertificateRepo
type MiAndGoogleCertificateRepo CertificateRepo
type OppoCertificateRepo CertificateRepo
type VivoCertificateRepo CertificateRepo

type CertificateUseCase struct {
	repo        CertificateRepo
	huawei      HuaweiCertificateRepo
	ios         IosCertificateRepo
	iosP8       IosP8CertificateRepo
	meizu       MeizuCertificateRepo
	miAndGoogle MiAndGoogleCertificateRepo
	oppo        OppoCertificateRepo
	vivo        VivoCertificateRepo
	log         *log.Helper
}

func NewCertificateUseCase(repo CertificateRepo, huawei HuaweiCertificateRepo,
	ios IosCertificateRepo,
	iosP8 IosP8CertificateRepo,
	meizu MeizuCertificateRepo,
	miAndGoogle MiAndGoogleCertificateRepo,
	oppo OppoCertificateRepo,
	vivo VivoCertificateRepo,
	logger log.Logger) *CertificateUseCase {
	return &CertificateUseCase{repo,
		huawei,
		ios,
		iosP8,
		meizu,
		miAndGoogle,
		oppo,
		vivo,
		log.NewHelper(logger)}
}

你可能感兴趣的:(go,wire,golang)