Skip to content
This repository was archived by the owner on Aug 25, 2025. It is now read-only.
This repository was archived by the owner on Aug 25, 2025. It is now read-only.

Types that implement the interface support dynamic bulk injection. #416

Description

@zhanjunjie2019

name: zhanjunjie
about: Suggest an idea for this project


Is your feature request related to a problem? Please describe.

There is an interface definition, and several implementation classes that implement this interface. In the constructor of another implementation class, all known instances of the implemented types need to be injected into this interface in the form of an array. The code is as follows:

// Interface and Implementation Class

type IAbs interface {
	Print()
}

type FirstAbsImpl struct {
}

func (f FirstAbsImpl) Print() {
	fmt.Println("I am FirstAbsImpl")
}

type SecondAbsImpl struct {
}

func (s SecondAbsImpl) Print() {
	fmt.Println("I am SecondAbsImpl")
}

// App

type App struct {
	abs []IAbs
}

func (a App) Prints() {
	for _, ab := range a.abs {
		ab.Print()
	}
}

Describe the solution you'd like

I hope to avoid the need to specify a particular location to track all the constructors of implementation classes. Instead, I would like the implementation classes to register themselves and allow injection in the form of an array or any single instance type.

Describe alternatives you've considered

// Interface and Implementation Class

var abs []IAbs

type IAbs interface {
	Print()
}

func init() {
	abs = append(abs, &FirstAbsImpl{})
}

type FirstAbsImpl struct {
}

func (f FirstAbsImpl) Print() {
	fmt.Println("I am FirstAbsImpl")
}

func init() {
	abs = append(abs, &SecondAbsImpl{})
}

type SecondAbsImpl struct {
}

func (s SecondAbsImpl) Print() {
	fmt.Println("I am SecondAbsImpl")
}

// App

type App struct {
}

func (a App) Prints() {
	for _, ab := range abs {
		ab.Print()
	}
}

Additional context

Add any other context or screenshots about the feature request here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions