7.3 Interface Satisfaction
A type satisfies an interface if it possesses all the methods
the interface requires.
For example, an *os.File
satisfies io.Reader
, Writer
,
Closer
, and ReadWriter
.
A *bytes.Buffer
satisfies Reader
, Writer
,
and ReadWriter
, but does not satisfy Closer
because it does not have a Close
method.
As a shorthand, Go programmers often say that a concrete type “is a”
particular interface type, meaning that it satisfies the interface.
For example, a *bytes.Buffer
is an io.Writer
; an
*os.File
is an io.ReadWriter
.
The assignability rule (§2.4.2) for interfaces is very simple: an expression may be assigned to an interface only if its type satisfies the interface. So:
var w io.Writer w = os.Stdout ...
Get The Go Programming Language now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.