Interface satisfaction
A type satisfies an interface when its method set contains every method of the interface, with matching names and signatures. Satisfaction is checked at compile time on assignment, not at the type's declaration.
Detailed Explanation
"Does *os.File implement io.Writer?" means "does the method set of *os.File include Write([]byte) (int, error)?" Yes. The assignment var w io.Writer = f is the check. There is no registry and no implements clause. Satisfaction is a property of two method sets.
Method sets in one paragraph. The method set of T is the methods declared with receiver T, plus promoted methods from embedding. The method set of *T is those plus the methods declared with receiver *T. Value-receiver methods are in both sets. Pointer-receiver methods are only in *T's set. That is why Session{} may fail to assign to an interface that *Session satisfies.
Signatures must match, not "look close." Write([]byte) error is not Write([]byte) (int, error). Unexported methods can satisfy an interface only if the interface method is also unexported and in the same package — which is why you should not put unexported methods on an exported interface you expect others to implement.
Assignable extra rules. A named interface value can be assigned to another interface if the first's method set is a superset. io.ReadWriter assigns to io.Writer. A type and a pointer to that type are different when their method sets differ. Untyped nil assigns to any interface and produces a nil interface.
Runtime satisfaction. x.(io.Writer) asks the same question about the dynamic type already stored in x. Compile-time satisfaction is assignment. Runtime satisfaction is assertion. Both use method sets.
What it costs. The check is compile-time for assignments and a method-set compare for assertions. The design cost is keeping method sets intentional: a pointer receiver you added for mutation can silently stop a value from satisfying an interface. The var _ Interface = (*T)(nil) line makes that failure local to the type's file.
Code Examples
package main
import "io"
type buf struct{ b []byte }
func (b *buf) Write(p []byte) (int, error) {
b.b = append(b.b, p...)
return len(p), nil
}
var _ io.Writer = (*buf)(nil)
func main() {
var w io.Writer = &buf{}
_, _ = w.Write([]byte("ok"))
}type Expirer interface{ Expired() bool }
type Session struct{}
func (s Session) Expired() bool { return false }
func values() {
var e Expirer
e = Session{} // ok: value receiver
e = &Session{} // ok: *T includes T's methods
_ = e
}func (s *Session) Refresh() {}
type Refresher interface{ Refresh() }
func pointers() {
var r Refresher
// r = Session{} // compile error: Refresh is on *Session
r = &Session{}
_ = r
}Common Gotchas
An unexported interface method cannot be implemented from another package
keep exported interfaces' methods exported.
Promoted methods count as satisfaction
embedding can make a type implement `io.Reader` without a `Read` you wrote.
`var _ I = T{}` documents the intent
fail the build in the implementing package, not in a distant caller.