Skip to content

Using dig.As() to provide the same interface to 2 different concrete types #392

Answered by abhinav
stepanen asked this question in Q&A
Discussion options

You must be logged in to vote

Hey, @stepanen. Yes, Dig allows that but exactly how/what depends on how you're using them. Some of the options are:

Value groups

If you're using them all together, e.g. at some point, you build a []Receiver and do something with it, you'll want to use value groups. These allow multiple components to provide the same thing to a named group, and then another component to request that entire named group.

For example,

container.Provide(NewReceiveOperationV1, dig.As(new(Receiver)), dig.Group("foo"))
container.Provide(NewReceiveOperationV2, dig.As(new(Receiver))), dig.Group("foo"))

// Elsewhere:

type fooParams struct {
  dig.In

  Receivers []Receiver `group:"foo"`
}

containers.Provide(func(p 

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by stepanen
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants