Skip to content
tezvyn:
Menu

Implicit Interface Satisfaction in Go

EasyHow cards are made

Go types satisfy an interface automatically by having the right methods, with no explicit implements declaration. This structural typing decouples implementations from interface definitions, so you can define interfaces around how you use a type without…

Why it exists

Many languages require a type to explicitly declare that it implements an interface, which couples the implementer to every interface it might satisfy and forces interface decisions up front. Go avoids this so that interfaces can be small, defined where they are consumed, and satisfied by types that were written without knowledge of them.

The mental model

Think of an interface as a checklist of method signatures. Any type whose method set contains all those signatures satisfies the interface automatically. There is no implements keyword and no link recorded in the type's definition; satisfaction is checked structurally by the compiler wherever the type is used as the interface.

How it works

The compiler verifies, at the point you assign or pass a concrete type where an interface is expected, that the type's method set covers the interface. If it does, the assignment compiles and an interface value holding the concrete type and its method table is created. Because the check is structural, the same type can satisfy many unrelated interfaces, and you can write an interface after the types that satisfy it already exist.

When it matters

It matters most for decoupling and testing. You define a tiny interface like one with a single Read method right next to the function that needs it, and pass real or fake implementations interchangeably. It enables Go's preference for small interfaces and the standard library's reuse of io.Reader and io.Writer everywhere.

A concrete example

The standard io.Writer interface declares one method, Write taking a byte slice and returning an int and an error. A bytes.Buffer, an os.File, and an http.ResponseWriter all have that method, so all three satisfy io.Writer without ever mentioning it. A function accepting an io.Writer can write to a file, a network response, or an in-memory buffer in tests, and none of those types were modified to opt in.

Interview question

A Go type defined in one package satisfies an interface declared later in another package, with no edits to the type. How is this possible?

  • a.The interface must be declared in the same package as the type
  • b.Go records interface satisfaction in a global registry at startup
  • c.The type must be recompiled with an implements directive
  • d.Satisfaction is structural: having the required methods is enough, checked at point of useCorrect
Why?

Go uses structural typing, so a type satisfies an interface merely by having the matching method set, verified where it is used. There is no implements keyword, registry, or same-package requirement.

Just read this? Test yourself on what you have been reading.

Read the original → go.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on go — each one lists the topics its interview covers.

See open roles