Replies: 1 comment 2 replies
-
I add the interfaces to the root so they can be shared by the subpackages. If the interface lived in the subpackage then you can get in situations where you have circular dependencies. However, the root doesn't import subpackages so it doesn't suffer from the circular dependency issue. Another option is to not use interfaces at all. For example, if you just have one database implementation (e.g.
You can define smaller interfaces but I just don't personally like doing it. I find it's a lot of extra types without a lot of value. |
Beta Was this translation helpful? Give feedback.
-
Hey I'm a fan of this structure and find it greatly simplifies projects, especially when starting out, while remaining useful for a large number of use cases. I'm curious why you advocate defining an interface for the service in the root as opposed to in subpackages that implement the interface? I was under the impression that in Go interfaces are best defined next to where they are consumed and used. This would also mean that if you don't need 1 or 2 functions from the root interface, you would need to still add those functions to the struct passed in to implement it in a subpackage with a dummy implementation or something like
panic("not implemented")
.Would it be equally feasible to not define the interfaces in the root and define smaller interfaces in subpackages? Or would you advise against that for smaller projects? For instance, instead of using a
UserService
interface you might have something smaller in a subpackage like:Beta Was this translation helpful? Give feedback.
All reactions