1
0
mirror of https://github.com/golang/go synced 2024-11-21 16:34:42 -07:00

effective go: explain about values/pointers in String() example

Fixes #1796.

R=rsc, r2, niemeyer
CC=golang-dev
https://golang.org/cl/4539049
This commit is contained in:
Rob Pike 2011-05-11 08:31:24 -07:00
parent 55d43f0ce8
commit 89c59bc6f6

View File

@ -1489,6 +1489,12 @@ to print in the format
7/-2.35/"abc\tdef"
</pre>
<p>
(If you need to print <em>values</em> of type <code>T</code> as well as pointers to <code>T</code>,
the receiver for <code>String</code> must be of value type; this example used a pointer because
that's more efficient and idiomatic for struct types.
See the section below on <a href="#pointers_vs_values">pointers vs. value receivers</a> for more information.)
</p>
<p>
Our <code>String()</code> method is able to call <code>Sprintf</code> because the
print routines are fully reentrant and can be used recursively.
We can even go one step further and pass a print routine's arguments directly to another such routine.