By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
An interface names a set of methods. Any type with that method set satisfies it automatically — there is no implements keyword and no registry. That single design decision is why Go code composes so well: a struct written today can satisfy an interface defined later, including one you do not own. The standard library leans on it everywhere — io.Writer, http.Handler, error itself are one-or-two-method interfaces.
Define interfaces where they are used, not where they are implemented. Relay's ingestion loop only needs a Sink with a single Write method; the in-memory counter satisfies it without importing the ingestion package. Keep interfaces small: the bigger the method set, the fewer types can satisfy it and the harder it is to fake in a test.
Embedding an interface in a struct lets the struct satisfy that interface while overriding or decorating some methods. That is exactly how middleware wraps an http.Handler, and how a test double records calls while delegating the rest. Composition beats inheritance here because behavior is assembled at the call site instead of fixed by a class tree.
package main
import (
"fmt"
"slices"
"strings"
)
type event struct {
Service string
Level string
}
// Sink is intentionally tiny: anything that can accept an event qualifies.
type Sink interface {
Write(event) error
}
type counter struct {
counts map[string]int
}
func newCounter() *counter {
return &counter{counts: map[string]int{}}
}
func (c *counter) Write(e event) error {
if e.Service == "" {
return fmt.Errorf("event has no service")
}
c.counts[e.Service]++
return nil
}
func (c *counter) Report() string {
keys := make([]string, 0, len(c.counts))
for key := range c.counts {
keys = append(keys, key)
}
slices.Sort(keys)
parts := make([]string, 0, len(keys))
for _, key := range keys {
parts = append(parts, fmt.Sprintf("%s=%d", key, c.counts[key]))
}
return strings.Join(parts, " ")
}
type discard struct{}
func (discard) Write(event) error { return nil }
func feed(s Sink, events []event) (int, error) {
accepted := 0
for _, e := range events {
if err := s.Write(e); err != nil {
return accepted, fmt.Errorf("write event %d: %w", accepted, err)
}
accepted++
}
return accepted, nil
}
func main() {
c := newCounter()
events := []event{
{Service: "api", Level: "info"},
{Service: "worker", Level: "warn"},
{Service: "", Level: "error"},
}
accepted, err := feed(c, events)
fmt.Println("accepted", accepted, "error", err)
fmt.Println("report:", c.Report())
var silent Sink = discard{}
if _, err := feed(silent, events); err == nil {
fmt.Println("discard accepts everything")
}
}An interface value is a pair of type and value. It is only nil when both are nil. A typed nil pointer stored in an interface is not nil, so if err != nil passes even though the underlying pointer is nil. This is the most common interface bug in Go: return a plain nil from a function whose return type is an interface, and never wrap a typed nil unless you mean it.
| Guideline | Good | Why |
|---|---|---|
| Small interfaces | type Sink interface { Write(event) error } | Easy to satisfy and easy to fake |
| Define at the consumer | feed(s Sink, ...) | The implementation does not import its caller |
| Accept interfaces, return structs | func New() *counter | Callers keep full behavior and can still abstract |
| Compose by embedding | struct { Sink } | Decorate one method, delegate the rest |
Avoid any as a design | any only at true boundaries | Type safety is the point of the type system |
| Nil check before use | if s == nil { ... } | A nil interface panics on the first method call |
Key takeaways
Interfaces are implicit method sets — no declaration of intent is required.
Define small interfaces where they are consumed; accept interfaces and return concrete types.
Composition through embedding lets a struct decorate or override part of an interface.
A typed nil inside an interface is not a nil interface; debug with %T.
Type assertions (v, ok := x.(T)) and type switches recover concrete behavior at the boundary.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.