Go Interfaces
Go interface patterns covering implicit implementation, composition, duck typing, and the 'accept interfaces, return structs' principle.
- Difficulty
- intermediate
- Read time
- 1 min read
- Version
- v1.0.0
- Confidence
- established
- Last updated
Quick Reference
Go Interfaces: Implicit implementation (no 'implements'). Small interfaces preferred (io.Reader = 1 method). Accept interfaces, return structs. Define interfaces at consumer, not producer. Composition over inheritance (embed interfaces). Use var _ Interface = (*Type)(nil) for compile-time checks.
Use When
- Go application design
- Dependency injection
- Testing with mocks
- API design
Skip When
- Non-Go projects
- Simple scripts
Go Interfaces
Go interface patterns covering implicit implementation, composition, duck typing, and the ‘accept interfaces, return structs’ principle.