Go · Stage 9 – Organizing Bigger Programs · Lesson 54 of 64

Interfaces as Behavior Contracts

Understand an interface as a small contract of required methods.

Go code

type Speaker interface {
    Speak() string
}
Line-by-line explanation
  1. Speaker is an interface type.
  2. Its contract requires one Speak method.
  3. That method must return a string.
Expected output
(no output)

The interface defines a contract but performs no action.

Go uses implicit satisfaction: a type does not declare that it implements an interface. Having the required methods is enough.