Interface — behavior contract via method signatures. Struct — data container with named fields.
Interfaces are implicitly satisfied. Any type with the right methods implements the interface.
1type Notifier interface {2 Notify(msg string) error3}45type EmailNotifier struct{ Addr string }6func (e EmailNotifier) Notify(msg string) error {7 // send email8 return nil9}
Structs hold data and implement interfaces.
1type SlackNotifier struct{ Channel string }2func (s SlackNotifier) Notify(msg string) error {3 // send slack message4 return nil5}
When to use:
Common mistakes:
any.