1
\$\begingroup\$

I'm developing a Go application that has lots of packages.
Many of the packages use the same type from a 3rd party library.
Should an interface be defined for the type in each package, and let the interfaces only contain the methods that are used by the package?
Or, we should define a single interface for the whole application, and let the interface contain the union of all the methods that are used throughout the application?
At official Golang wiki, there's a statement

Go interfaces generally belong in the package that uses values of the interface type, not the package that implements those values.

But does the package means the one that actually uses the type or the root package of the project?
Here's a simplified example:

package subnet
type netOpsClient interface {
 Subnets(ctx context.Context, options map[string]string) ([]string, error)
}
type PrimaryNetFinder struct {
 netOpsClient netOpsClient
}
func (p *PrimaryNetFinder) Find(ctx context.Context) ([]string, error) {
 subnets, err := p.netOpsClient.Subnets(ctx, map[string]string{
 "level": "50",
 })
 //...
}
package loader
type netOpsClient interface {
 Subnets(ctx context.Context, options map[string]string) ([]string, error)
 SubnetGet(ctx context.Context, id string) (netops.Subnet, error)
}
type RulesLoader struct {
 netOpsClient netOpsClient
 policyStore policyStore
}
func (r *RulesLoader) LoadRules(ctx context.Context) error {
 //...
 subnets, err := r.netOpsClient.Subnets(ctx, map[string]string{
 "level": level,
 "site": "ne",
 })
 //...
 subnet, err := r.netOpsClient.SubnetGet(ctx, subnetId)
 //...
 policies, err := policyStore.Policies(subnet.cidr)
 //...
}

You can see both PrimaryNetFinder and RulesLoader store a field of type netOpsClient, which is an interface defined in the package. And a type from a 3-party NetOpsClient implements the interface.
If many packages in the project have a reference to the type, should each package define a similar interface like above? Or, should I define a single interface, and let all the packages share the same interface like below?

package netops
type NetOpsClient interface {
 Subnets(ctx context.Context, options map[string]string) ([]string, error)
 SubnetGet(ctx context.Context, id string) (netops.Subnet, error)
 // ...
}
asked Feb 10, 2020 at 3:18
\$\endgroup\$
1

1 Answer 1

4
\$\begingroup\$

Personally I would define it in each package that uses it. It would only be a small amount of duplication but has many advantages:

  1. It keeps the package self contained.
  2. You mentioned that not all packages use the same method so it stops the interface getting very large.
  3. If not every package uses each method it makes it explicit which method a package does use.
answered Feb 10, 2020 at 7:01
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.