Implicit implementation
A type implements an interface by having the required methods. You never write implements. The compiler checks the method set at every assignment to an interface variable.
Detailed Explanation
In Java, a class lists implements Writer. In Go, *os.File does not mention io.Writer. The assignment var w io.Writer = f compiles because *os.File has Write. If it does not, you get a compile error at the assignment, not at the type declaration.
Why implicit. Libraries can satisfy interfaces they have never imported. You define type Store interface { Find(id string) (User, error) } in your HTTP package. A concrete type in the database package grows a Find method. The HTTP package accepts it without the database package depending on HTTP. That missing dependency is the feature. It is also why standard-library types keep growing new interface implementations years later without editing those types.
The check is structural. Names and signatures must match exactly: Write([]byte) (int, error), not Write([]byte) error. A pointer receiver vs value receiver still follows method-set rules. Extra methods on the concrete type are fine. Missing methods are not.
Compile-time assertion. var _ io.Writer = (*Buffer)(nil) is a common line in a package that wants a broken method set to fail at compile time, even if nobody has assigned a Buffer to a Writer yet. The blank identifier means "do not keep this value; only type-check it."
No accidental "I implement too much" keyword. Because implementation is implicit, embedding a type with a large method set can make your struct satisfy interfaces you did not intend (io.Reader from an embedded bytes.Buffer). That is the downside of not writing implements: satisfaction is a property of the method set, not of an author declaration.
What it costs. Implicit satisfaction decouples packages. The cost is discoverability: "what implements Store?" is answered by a method-set search, not by a list on the type. Editors and guru-style tools help. For public APIs, keep interfaces small so the search stays obvious.
Code Examples
package main
import (
"fmt"
"io"
"strings"
)
type upper struct{ s string }
func (u upper) Read(p []byte) (int, error) {
return strings.NewReader(strings.ToUpper(u.s)).Read(p)
}
func main() {
var r io.Reader = upper{s: "hi"}
buf := make([]byte, 2)
n, _ := r.Read(buf)
fmt.Println(string(buf[:n]))
}type Buffer struct{}
func (b *Buffer) Write(p []byte) (int, error) { return len(p), nil }
var _ io.Writer = (*Buffer)(nil) // fails to compile if Write is missing or wrongtype Store interface {
Find(id string) error
}
type Memory struct{}
func (Memory) Find(id string) error { return nil }
func use(s Store) {}
func main() { use(Memory{}) }Common Gotchas
A wrong signature is not an implementation
`Write([]byte) error` does not satisfy `io.Writer`. The error appears at assignment, which can be far from the method.
Pointer vs value method sets still apply
`Memory{}` does not satisfy an interface whose methods are all on `*Memory`.
Embedding can implement interfaces by accident
promoted methods count.