Skip to content

Commit 2fcb25e

Browse files
author
Bryan C. Mills
committed
doc/effective_go: clarify advice on returning interfaces
New Gophers sometimes misconstrue the advice in the "Generality" section as "export interfaces instead of implementations" and add needless interfaces to their code as a result. Down the road, they end up needing to add methods and either break existing callers or have to resort to unpleasant hacks (e.g. using "magic method" type-switches). Weaken the first paragraph of this section to only advise leaving types unexported when they will never need additional methods. Change-Id: I32a1ae44012b5896faf167c02e192398a4dfc0b8 Reviewed-on: https://go-review.googlesource.com/24892 Reviewed-by: Rob Pike <[email protected]> Reviewed-by: Andrew Gerrand <[email protected]>
1 parent a1110c3 commit 2fcb25e

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

doc/effective_go.html

+6-8
Original file line numberDiff line numberDiff line change
@@ -2238,13 +2238,12 @@ <h3 id="interface_conversions">Interface conversions and type assertions</h3>
22382238

22392239
<h3 id="generality">Generality</h3>
22402240
<p>
2241-
If a type exists only to implement an interface
2242-
and has no exported methods beyond that interface,
2243-
there is no need to export the type itself.
2244-
Exporting just the interface makes it clear that
2245-
it's the behavior that matters, not the implementation,
2246-
and that other implementations with different properties
2247-
can mirror the behavior of the original type.
2241+
If a type exists only to implement an interface and will
2242+
never have exported methods beyond that interface, there is
2243+
no need to export the type itself.
2244+
Exporting just the interface makes it clear the value has no
2245+
interesting behavior beyond what is described in the
2246+
interface.
22482247
It also avoids the need to repeat the documentation
22492248
on every instance of a common method.
22502249
</p>
@@ -3665,4 +3664,3 @@ <h2 id="web_server">A web server</h2>
36653664
var _ image.Image = Black
36663665
</pre>
36673666
-->
3668-

0 commit comments

Comments
 (0)