7.13 Type Switches
Interfaces are used in two distinct styles.
In the first style, exemplified by io.Reader
,
io.Writer
, fmt.Stringer
, sort.Interface
,
http.Handler
, and error
,
an interface’s methods express the similarities of
the concrete types that satisfy the interface but hide the
representation details and intrinsic operations of those concrete
types.
The emphasis is on the methods, not on the concrete types.
The second style exploits the ability of an interface value to hold values of a variety of concrete types and considers the interface to be the union of those types. Type assertions are used to discriminate among these types dynamically and treat each case differently. In this style, the emphasis is on the concrete types that satisfy ...
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.