When you finish your package, you go down the package tree and implement the interfaces you defined in the top-level package. These implementations must satisfy specific interfaces of the top-level package, which means they won't be very reusable. It happens because of two reasons.
- You follow the signature that was defined in the top-level package. However, other potential consumers define their own interfaces, and their signatures might not be the same.
- You import types (for example, 'Entity' in the example above) in your implementation to satisfy the interface. This type belongs to your top-level package.
It's important to note that Golang implements interfaces implicitly
, and this is the explanation
for this design on the tour.golang.org
website:"A type implements an interface by implementing its methods. There is no explicit declaration of intent, no "implements" keyword. Implicit interfaces decouple the definition of an interface from its implementation, which could then appear in any package without prearrangement."
This explanation is a little misleading because you must import types from the package that declares an interface anyway, for example, Entity
. The only case when your implementation can really appear in any package without rearrangement, as claimed above, is when signatures of methods contain only primitive types, for example: