What "Accept Interfaces, Return Structs" Means and Why

Accept interfaces as parameters for flexibility and return concrete structs to hide implementation details and maintain API stability.

The principle "Accept Interfaces, Return Structs" means functions should take interfaces as parameters to allow flexible callers but return concrete structs to prevent callers from depending on implementation details. This design keeps the API stable and decouples the caller from the specific type used internally.

// Accept an interface for flexibility
func Process(r Reader) error {
    // ... implementation ...
    return nil
}

// Return a concrete struct to hide implementation
func NewProcessor() *Processor {
    return &Processor{}
}