From d4c23261dc940eb2cb841a1a84832174e6bac0c4 Mon Sep 17 00:00:00 2001 From: Rob Pike Date: Fri, 27 Sep 2013 22:12:31 +1000 Subject: 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 --- content/slices/prog140.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'content/slices/prog140.go') 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 } -- cgit v1.2.3