Saurabh Kumar

Go interfaces are implicit: a type satisfies an interface by having the right…

Go interfaces are implicit: a type satisfies an interface by having the right methods, no implements keyword.

  • Closest Python equivalent is typing.Protocol (structural subtyping, checked by mypy), but Go enforces it at compile time, not via a separate type checker.
  • Sharp edge: a nil concrete pointer wrapped in an interface value is not nil. var dog *Dog = nil; var s Speaker = dog gives s != nil because the interface holds (*Dog, nil), a typed pair. Bites hardest when returning error interfaces.
  • Go proverb: “the bigger the interface, the weaker the abstraction.” Keep them to 1-3 methods, define where consumed not where implemented.