Back to articles
Go interfaces: accept interfaces, return structs — and when not to

Go interfaces: accept interfaces, return structs — and when not to

via Dev.toOdilon HUGONNOT

During a code review on ClaudeGate, a colleague asked me: "why isn't your Store interface in the consumer package?" He was citing the Go convention "accept interfaces, return structs". He was right about the convention — and wrong about applying it in this specific case. It's one of those situations where knowing the rule isn't enough. You also need to understand why it exists to know when to ignore it without guilt. The convention explained from scratch In Java or C#, when you write a class, you explicitly declare the interfaces it implements. The producer decides: class SQLiteStore implements Store . The interface typically lives in the same package as the implementation. Go works the other way around. Interface implementation is implicit : if a type has the right methods, it satisfies the interface — no declaration, no implements keyword. And the convention that follows from this is that it's the consumer that defines the interface it needs , not the producer. // ❌ Java-style: the p

Continue reading on Dev.to

Opens in a new tab

Read Full Article
0 views

Related Articles