[x/blog] go.blog/slices: fix bug in Append

The code didn't work but the blog runs the previous simple version, which does,
so when you hit Run it looks like it's working.
It only had two bugs (so far).

Thanks to shelah.kell@gmail.com for noticing.

R=adg
CC=golang-dev
https://golang.org/cl/14036043
X-Blog-Commit: d4c23261dc940eb2cb841a1a84832174e6bac0c4
This commit is contained in:
Rob Pike 2013-09-27 22:12:31 +10:00
Родитель 35f822c532
Коммит 17bd7046c4
2 изменённых файлов: 4 добавлений и 4 удалений

Просмотреть файл

@ -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

Просмотреть файл

@ -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
}