What Is Interface Pollution in Go and How to Avoid It

Avoid interface pollution in Go by defining small, focused interfaces that only include the methods actually used by the consumer.

Interface pollution in Go occurs when an interface defines more methods than a specific implementation needs, forcing unrelated types to implement unnecessary methods just to satisfy the interface. To avoid this, define small, focused interfaces that describe only the behavior required by the consumer, rather than mirroring the full API of the implementation. For example, instead of requiring a full io.ReadWriteCloser when you only need to read, define a smaller interface:

type Reader interface {
    Read(p []byte) (n int, err error)
}

func Process(r Reader) {
    // Only uses Read
}

This allows any type with a Read method to satisfy the interface without needing Write or Close methods.