1
0
mirror of https://github.com/golang/go synced 2024-11-22 01:44:40 -07:00

be more explicit about initialization of embedded fields.

R=rsc
CC=golang-dev
https://golang.org/cl/186161
This commit is contained in:
Rob Pike 2010-01-15 11:59:53 +11:00
parent 8cf627ad57
commit 49a35a632e

View File

@ -1395,7 +1395,7 @@ func (b ByteSize) String() string {
</pre> </pre>
<p> <p>
The expression <code>YB</code> prints as <code>1.00YB</code>, The expression <code>YB</code> prints as <code>1.00YB</code>,
while <code>ByteSize(1e13)</code> prints as <code>9.09TB</code>, while <code>ByteSize(1e13)</code> prints as <code>9.09TB</code>.
</p> </p>
<h3 id="variables">Variables</h3> <h3 id="variables">Variables</h3>
@ -1878,12 +1878,15 @@ but does not give them field names.
// ReadWriter stores pointers to a Reader and a Writer. // ReadWriter stores pointers to a Reader and a Writer.
// It implements io.ReadWriter. // It implements io.ReadWriter.
type ReadWriter struct { type ReadWriter struct {
*Reader *Reader // *bufio.Reader
*Writer *Writer // *bufio.Writer
} }
</pre> </pre>
<p> <p>
This struct could be written as The embedded elements are pointers to structs and of course
must be initialized to point to valid structs before they
can be used.
The <code>ReadWriter</code> struct could be written as
</p> </p>
<pre> <pre>
type ReadWriter struct { type ReadWriter struct {
@ -1933,15 +1936,16 @@ type Job struct {
The <code>Job</code> type now has the <code>Log</code>, <code>Logf</code> The <code>Job</code> type now has the <code>Log</code>, <code>Logf</code>
and other and other
methods of <code>log.Logger</code>. We could have given the <code>Logger</code> methods of <code>log.Logger</code>. We could have given the <code>Logger</code>
a field name, of course, but it's not necessary to do so. And now we can a field name, of course, but it's not necessary to do so. And now, once
log to a <code>Job</code>: initialized, we can
log to the <code>Job</code>:
</p> </p>
<pre> <pre>
job.Log("starting now...") job.Log("starting now...")
</pre> </pre>
<p> <p>
The <code>Logger</code> is a regular field of the struct and we can initialize The <code>Logger</code> is a regular field of the struct and we can initialize
it in the usual way. it in the usual way with a constructor,
</p> </p>
<pre> <pre>
func NewJob(command string, logger *log.Logger) *Job { func NewJob(command string, logger *log.Logger) *Job {
@ -1949,6 +1953,12 @@ func NewJob(command string, logger *log.Logger) *Job {
} }
</pre> </pre>
<p> <p>
or with a composite literal,
</p>
<pre>
job := &amp;Job{command, log.New(os.Stderr, nil, "Job: ", log.Ldate)}
</pre>
<p>
If we need to refer to an embedded field directly, the type name of the field, If we need to refer to an embedded field directly, the type name of the field,
ignoring the package qualifier, serves as a field name. If we needed to access the ignoring the package qualifier, serves as a field name. If we needed to access the
<code>*log.Logger</code> of a <code>Job</code> variable <code>job</code>, <code>*log.Logger</code> of a <code>Job</code> variable <code>job</code>,