1. Why a “small interface” is not aesthetics, but the price of a contract
When you first hear “make interfaces small,” it sounds like another religion: some people like tabs, others spaces, and others like small interfaces. But in Go this is not about taste. An interface is a contract: if you add a method to it, you require all implementations to support that method, otherwise the code stops compiling.
Imagine that an interface is not a list of methods, but a utility bill. The more items on the bill, the more you pay every month: implementations need more code, more tests, more behavior variants, and more places where things can go wrong.
To make it more concrete, here is a small “cost” table:
| Interface size | How it feels in the project | Typical result |
|---|---|---|
| 1–2 methods | easy to implement and replace | flexibility without pain |
| 3–5 methods | sometimes fine, but you already need to think | quickly becomes “everyone needs everything” |
| 6+ methods | almost always a “kitchen sink” | hard to implement, scary to change |
Go has a good practical motivator: “public contracts are hard to change.” For example, in the official Go Blog discussion about errors, the idea is emphasized that when you “expose” details, you make them part of the API, and later it becomes harder to change the implementation without breaking clients.
Interfaces are very similar: a big interface is a large public surface that you are now required to support.
2. An interface should describe the code’s need, not “the capabilities of the universe”
A common beginner mistake: “I’ll invent the perfect interface for the future so it can do everything.” It sounds noble, but in practice it turns into the interface describing not the need of a specific place in the code, but the author’s fantasies about what might someday be useful to somebody.
In Go it is more convenient to think the other way around: an interface is born where the code needs some behavior. The code says: “I need to be able to add a task” — so we need an interface with one method, Add. The code says: “I need to be able to get a list of tasks” — another interface. And that is it, no philosophy.
Let’s start growing our training application (a task manager of sorts). We already have a model:
package main
type Task struct {
ID int
Title string
Done bool
}
Now imagine that we have a function that prints tasks. It does not care at all how tasks are stored: in memory, in a file, in a database, or in the cat’s head. It cares about one thing: “give me a list.”
So here is the interface — minimal:
package main
type TaskLister interface {
List() []Task
}
And the consumer function:
package main
import "fmt"
func PrintTasks(l TaskLister) {
for _, t := range l.List() {
fmt.Println(t.ID, t.Title, t.Done) // 1 Buy bread false
}
}
Notice that PrintTasks does not ask to “be able to save,” “be able to delete,” “be able to export to JSON,” or “be able to sync with space.” It asks for exactly what it needs: List().
3. Separation of concerns: “reading separately, writing separately”
As a program grows, there is a temptation to make one TaskService interface that can do everything: add, delete, mark done, search, print statistics, make coffee. The problem is that different parts of the program use different pieces of this “everything.”
In Go, the typical approach is to split the contract by responsibilities. That does not mean the implementation should be scattered. On the contrary: one implementation can support many small interfaces at the same time. Consumers just take what they need and do not drag the rest around like a trailer.
Let’s introduce a few small interfaces for tasks:
package main
type TaskAdder interface {
Add(title string) (Task, error)
}
type TaskCompleter interface {
MarkDone(id int) error
}
type TaskFinder interface {
FindByID(id int) (Task, bool)
}
Now the code that adds a task does not need to know about “mark done.” The code that marks done does not need to know how to search by ID (though sometimes it is convenient). We are not preventing implementations from doing more — we are preventing the consumer from demanding more than it needs.
If you want to see it as “wiring,” it can look like this:
AddTaskFlow ---> TaskAdder
DoneTaskFlow ---> TaskCompleter
ShowTaskFlow ---> TaskFinder
Consumers depend on the minimal contract. The implementation can be the same, but the dependencies become thinner.
Naming small interfaces: “what it does,” not “what it is”
When an interface is small, its name is usually very simple. And here Go has a stable style: the name often describes a role/action, often with the -er suffix (Reader, Writer, Stringer). But that is not a law, just a useful habit: the name should help read the code like a sentence.
Compare the feeling:
package main
type TaskService interface {
Add(title string) (Task, error)
List() []Task
MarkDone(id int) error
}
and
package main
type TaskAdder interface {
Add(title string) (Task, error)
}
In the second case it is immediately clear: “this is something that can add a task.” Not “service,” not “manager,” not “component” (words that mean roughly nothing).
A decent name test: try mentally inserting it into a sentence.
- func RunAddCommand(a TaskAdder) error reads fine.
- func RunAddCommand(s TaskService) error reads... well, “service,” and service, and so what.
Interface composition: you can build bigger ones from small ones
Interfaces in Go can be embedded in one another. This is convenient when you sometimes need a broader contract, but want to keep the building blocks small and reusable.
In our application, sometimes we may need a “full task store” that can add, list, and mark done. Then we can assemble an interface from parts:
package main
type TaskStore interface {
TaskAdder
TaskLister
TaskCompleter
}
The point here is subtle: TaskStore is not the “main interface of the world,” but a convenient assembly for the places where you really need everything at once. At the same time, if some code only needs TaskLister, it does not have to accept TaskStore.
And here is the important part: composition is a way not to duplicate signatures, not a way to “legally make a huge interface.” If you feel that TaskStore is growing to 12 methods, that is often a signal that you have mixed responsibilities and need to decompose into more independent roles.
4. Practice: one implementation — many small interfaces
Now let’s make a simple in-memory task store implementation. It will be one type that does everything needed, but consumers will only see the methods they requested through small interfaces.
Let’s start with the structure:
package main
type InMemoryTasks struct {
nextID int
items []Task
}
Let’s add the Add method:
package main
import "errors"
func (s *InMemoryTasks) Add(title string) (Task, error) {
if title == "" {
return Task{}, errors.New("title is empty")
}
s.nextID++
t := Task{ID: s.nextID, Title: title, Done: false}
s.items = append(s.items, t)
return t, nil
}
Now List:
package main
func (s *InMemoryTasks) List() []Task {
return s.items
}
And MarkDone:
package main
import "errors"
func (s *InMemoryTasks) MarkDone(id int) error {
for i := range s.items {
if s.items[i].ID == id {
s.items[i].Done = true
return nil
}
}
return errors.New("task not found")
}
Now in main we can show that the same object fits different interfaces:
package main
import "fmt"
func main() {
store := &InMemoryTasks{}
_, _ = store.Add("Buy bread")
_, _ = store.Add("Learn interfaces (small ones!)")
PrintTasks(store)
_ = store.MarkDone(1)
fmt.Println("---")
PrintTasks(store)
}
Here the magic (actually, strict typing) is that PrintTasks accepts TaskLister, and *InMemoryTasks automatically fits because it has List() []Task.
5. How to tell when an interface has become too large
There are several “smells” by which a large interface can be noticed before it bites you.
The first smell is implementations with “empty methods.” When you see code like “to satisfy the interface, I’ll implement a method that always returns an error,” that almost always means the interface is demanding something from the implementation that is not natural for it.
Imagine you made a huge interface:
package main
type MegaTaskService interface {
Add(title string) (Task, error)
List() []Task
MarkDone(id int) error
ExportCSV() (string, error)
ImportCSV(csv string) error
}
And then you wanted to make a simple in-memory implementation without import/export. And the circus begins: ExportCSV is not needed, but the interface forces you to write it. In the end you either build stubs, or postpone useful implementation, or do “export” via panic (which is even more fun, but only once).
The second smell is an interface dragging a bunch of dependencies with it. For example, you added a method that returns some complex type, and now all implementations must import a package they do not need at all. In Go this is usually considered bad form: an interface should be light so that it is easy to implement.
The third smell is an interface that nobody uses as a whole. If in code you constantly write functions that need MegaTaskService, but inside they only use List, that is a signal: the interface is too fat for the consumer.
What should you do? Usually a simple refactoring helps: name the need and extract a small interface. Example: instead of MegaTaskService, make TaskLister, TaskAdder, TaskCompleter, and leave the “big assembly” only where a set of operations is actually needed.
Interface dieting in code: minimal contracts for commands
To reinforce the idea, let’s imagine (for now without a real CLI on flag, that will come later) that we have an “add command” and a “show list command.” Each of them should depend on the minimal set of methods.
Add command:
package main
import "fmt"
func RunAdd(a TaskAdder, title string) error {
t, err := a.Add(title)
if err != nil {
return err
}
fmt.Println("added:", t.ID, t.Title) // added: 1 Buy bread
return nil
}
List command:
package main
func RunList(l TaskLister) {
PrintTasks(l)
}
And now the important part: if tomorrow another way of storing tasks appears (not in memory), RunAdd and RunList do not need to change. They depend on behavior, not on a concrete type. And if the new implementation somehow has no “mark done,” that does not affect RunList. That is exactly the practical value of small interfaces.
6. Common mistakes when designing small interfaces
Mistake #1: “I’ll make one interface for everything, we’ll sort it out later.”
From the outside it seems simpler: one interface, one variable, one dependency. But in a couple of weeks it turns out that different parts of the program use different methods, and implementations start to either bloat or write stubs. In the end, “simplicity” turns into a sticky lump that is hard to roll through the codebase.
Mistake #2: the interface describes data, not behavior.
Sometimes you want to make an interface like type Task interface { ID() int; Title() string; Done() bool }, and then expect it to be a “universal task.” In practice you often get extra indirection: instead of a normal struct with fields, you are forced to go through methods everywhere, and the code becomes more verbose. It is better to introduce an interface where behavior replaceability is really needed, not because “that is more object-oriented.”
Mistake #3: the interface pulls in extra dependencies through signatures.
If an interface method returns a specific type from a large package, you force all implementations to know that package. It is like asking someone to “just bring water,” but also making them get a truck license. Interface signatures should be as simple as is reasonable.
Mistake #4: overly general names like Service, Manager, Provider.
Such names do a poor job of helping read the code: they do not say what exactly the dependency can do. With small interfaces it usually works the other way around: a name like TaskAdder or TaskLister makes the code self-explanatory, and you have to “dig” into the implementation less often to understand the meaning.
Mistake #5: a “small interface” with “just in case” methods.
Sometimes an interface with 2–3 methods already feels “small,” and a third “just in case it becomes useful” gets added. In Go this is a slippery slope: one “just in case” quickly becomes five, and then you have a kitchen sink again, only you did not notice because it grew gradually.
GO TO FULL VERSION