aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--content/slices.article2
-rw-r--r--content/slices/prog140.go6
2 files changed, 4 insertions, 4 deletions
diff --git a/content/slices.article b/content/slices.article
index eb974e8..de76408 100644
--- a/content/slices.article
+++ b/content/slices.article
@@ -443,7 +443,7 @@ allocated memory, and then to copy the appending items to the end of the old dat
Try it; the behavior is the same as before:
-.play -edit slices/prog130.go /START/,/END/
+.play -edit slices/prog140.go /START/,/END/
* Append: The built-in function
diff --git a/content/slices/prog140.go b/content/slices/prog140.go
index 306d6f2..ab04d97 100644
--- a/content/slices/prog140.go
+++ b/content/slices/prog140.go
@@ -11,7 +11,8 @@ import (
// Append appends the elements to the slice.
// Efficient version.
func Append(slice []int, elements ...int) []int {
- total := len(slice) + len(items)
+ n := len(slice)
+ total := len(slice) + len(elements)
if total > cap(slice) {
// Reallocate. Grow to 1.5 times the new size, so we can still grow.
newSize := total*3/2 + 1
@@ -19,9 +20,8 @@ func Append(slice []int, elements ...int) []int {
copy(newSlice, slice)
slice = newSlice
}
- n := len(slice)
slice = slice[:total]
- copy(slice[:n], elements)
+ copy(slice[n:], elements)
return slice
}