Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
G
golang
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
go
golang
Commits
49a35a63
Commit
49a35a63
authored
Jan 15, 2010
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
be more explicit about initialization of embedded fields.
R=rsc CC=golang-dev
https://golang.org/cl/186161
parent
8cf627ad
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
7 deletions
+17
-7
effective_go.html
doc/effective_go.html
+17
-7
No files found.
doc/effective_go.html
View file @
49a35a63
...
...
@@ -1395,7 +1395,7 @@ func (b ByteSize) String() string {
</pre>
<p>
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>
<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.
// It implements io.ReadWriter.
type ReadWriter struct {
*Reader
*Writer
*Reader
// *bufio.Reader
*Writer
// *bufio.Writer
}
</pre>
<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>
<pre>
type ReadWriter struct {
...
...
@@ -1933,15 +1936,16 @@ type Job struct {
The
<code>
Job
</code>
type now has the
<code>
Log
</code>
,
<code>
Logf
</code>
and other
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
log to a
<code>
Job
</code>
:
a field name, of course, but it's not necessary to do so. And now, once
initialized, we can
log to the
<code>
Job
</code>
:
</p>
<pre>
job.Log("starting now...")
</pre>
<p>
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>
<pre>
func NewJob(command string, logger *log.Logger) *Job {
...
...
@@ -1949,6 +1953,12 @@ func NewJob(command string, logger *log.Logger) *Job {
}
</pre>
<p>
or with a composite literal,
</p>
<pre>
job :=
&
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,
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>
,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment