Interface — contract of method signatures defining behavior. Struct — named, typed fields defining data.
Interfaces enable polymorphism through implicit satisfaction. Any type with the right methods satisfies the interface — no declaration needed.
1type Formatter interface {2 Format() string3}45type JSONFormatter struct{ Data interface{} }6func (f JSONFormatter) Format() string {7 b, _ := json.Marshal(f.Data)8 return string(b)9}1011type TextFormatter struct{ Text string }12func (f TextFormatter) Format() string {13 return f.Text14}
Structs hold data and implement interfaces. They compose via embedding and support JSON/DB tags.
1type Base struct{ ID int }2type User struct {3 Base4 Name string5}
When to use:
Common mistakes:
any.