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
0808b199
Commit
0808b199
authored
Nov 02, 2010
by
Rob Pike
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Effective Go: append and a few words about ...
R=rsc, gri, iant CC=golang-dev
https://golang.org/cl/2821041
parent
c3328923
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
50 additions
and
0 deletions
+50
-0
effective_go.html
doc/effective_go.html
+50
-0
No files found.
doc/effective_go.html
View file @
0808b199
...
...
@@ -1218,6 +1218,11 @@ func Append(slice, data[]byte) []byte {
We must return the slice afterwards because, although
<code>
Append
</code>
can modify the elements of
<code>
slice
</code>
, the slice itself (the run-time data
structure holding the pointer, length, and capacity) is passed by value.
<p>
The idea of appending to a slice is so useful it's captured by the
<code>
append
</code>
built-in function. To understand that function's
design, though, we need a little more information, so we'll return
to it later.
</p>
...
...
@@ -1465,6 +1470,10 @@ func Println(v ...interface{}) {
}
</pre>
<p>
We write
<code>
...
</code>
after
<code>
v
</code>
in the call to
<code>
Output
</code>
to tell the
compiler to treat
<code>
v
</code>
as a list of arguments; otherwise it would just pass
<code>
v
</code>
as a single slice argument.
<p>
There's even more to printing than we've covered here. See the
<code>
godoc
</code>
documentation
for package
<code>
fmt
</code>
for the details.
</p>
...
...
@@ -1484,6 +1493,47 @@ func Min(a ...int) int {
}
</
pre
>
<h3
id=
"append"
>
Append
</h3>
<p>
Now we have the missing piece we needed to explain the design of
the
<code>
append
</code>
built-in function. The signature of
<code>
append
</code>
is different from our custom
<code>
Append
</code>
function above.
Schematically, it's like this:
<pre>
func append(slice []
<i>
T
</i>
, elements...T) []
<i>
T
</i>
</pre>
where
<i>
T
</i>
is a placeholder for any given type. You can't
actually write a function in Go where the type
<code>
T
</code>
is determined by the caller.
That's why
<code>
append
</code>
is built in: it needs support from the
compiler.
<p>
What
<code>
append
</code>
does is append the elements to the end of
the slice and return the result. The result needs to be returned
because, as with our hand-written
<code>
Append
</code>
, the underlying
array may change. This simple example
<pre>
x := []int{1,2,3}
x = append(x, 4, 5, 6)
fmt.Println(x)
</pre>
prints
<code>
[1 2 3 4 5 6]
</code>
. So
<code>
append
</code>
works a
little like
<code>
Printf
</code>
, collecting an arbitrary number of
arguments.
<p>
But what if we wanted to do what our
<code>
Append
</code>
does and
append a slice to a slice? Easy: use
<code>
...
</code>
at the call
site, just as we did in the call to
<code>
Output
</code>
above. This
snippet produces identical output to the one above.
<pre>
x := []int{1,2,3}
y := []int{4,5,6}
x = append(x, y...)
fmt.Println(x)
</pre>
Without that
<code>
...
</code>
, it wouldn't compile because the types
would be wrong;
<code>
y
</code>
is not of type
<code>
int
</code>
.
<h2
id=
"initialization"
>
Initialization
</h2>
<p>
...
...
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