Interface — defines behavior through method signatures, enabling polymorphism. Struct — defines data through named fields, providing concrete implementations.
Interfaces are implicitly satisfied in Go — if a type has all the required methods, it implements the interface. No declaration needed. This enables loose coupling and easy testing.
1type Speaker interface {2 Speak() string3}45type Dog struct{ Name string }6func (d Dog) Speak() string {7 return "Woof"8}910type Cat struct{ Name string }11func (c Cat) Speak() string {12 return "Meow"13}1415func makeSound(s Speaker) {16 fmt.Println(s.Speak())17}
Structs are concrete types with named fields. You attach methods to them and can embed other structs for composition.
1type Config struct {2 Debug bool3 Port int4}
When to use:
Common mistakes:
any (empty interface) — loses type safety.