Clean Interfaces

Designing clean abstractions is one of the most challenging parts of software engineering. Here are three core principles for Go interfaces.

1. Keep Interfaces Small

Interfaces should define the minimal behavior necessary to represent a concept. The smaller the interface, the easier it is for different types to implement it.

2. No Knowledge of Satisfying Types

An interface should not know which types implement it. It should define “what is needed,” not “who provides it.”

Anti-pattern:

type car interface {
    Speed() int
    IsFiretruck() bool // WRONG: Car shouldn't care if it's a firetruck
}

3. Interfaces Are Not Classes

  • Interfaces are slimmer than classes.
  • They define function signatures, but not underlying behavior.
  • They don’t have constructors or hierarchical inheritance in the classical sense.
  • They are used for composition over inheritance.