Senior
Where is it better to describe interfaces — on the source side or on the consumer side?
sobes.tech AI
Answer from AI
In Go, it is common to describe interfaces on the consumer side rather than the provider side. This allows for more flexible and extensible architectures, as the consumer defines which methods it needs, and the provider simply implements those methods. This approach reduces component coupling and simplifies testing.
Example:
// Interface is defined where it is used
type Reader interface {
Read(p []byte) (n int, err error)
}
// The source simply implements the interface methods
func (f *File) Read(p []byte) (n int, err error) {
// implementation
}
Thus, interfaces are described where they are needed, not where they are implemented.