1
0
mirror of https://github.com/golang/go synced 2024-11-21 22:54:40 -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" 7/-2.35/"abc\tdef"
</pre> </pre>
<p> <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 Our <code>String()</code> method is able to call <code>Sprintf</code> because the
print routines are fully reentrant and can be used recursively. 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. We can even go one step further and pass a print routine's arguments directly to another such routine.