Using Go interfaces effectively

"Accept interfaces, return structs."
- Jack Lindamood

The key point behind organizing code into packages is to make it reusable and available to external consumers in a frictionless way by providing a clean, well-documented API surface that consumers can build on. When authoring functions or methods that accept concrete types as arguments, we place an artificial constraint on the usefulness of our implementation: it only works with instances of a particular type.

While this may not always be an issue, in some cases, requiring a concrete type instance can potentially make testing non-trivial and slow, especially if the construction of such an instance is a costly operation. The following excerpt is part of a ...

Get Hands-On Software Engineering with Golang now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.