1
0
mirror of https://github.com/golang/go synced 2024-11-22 00:14:42 -07:00

Use the copy function rather than a loop.

R=r
CC=golang-dev
https://golang.org/cl/882047
This commit is contained in:
Ian Lance Taylor 2010-04-13 13:05:29 -07:00
parent d80c78b62f
commit cd242fb480

View File

@ -1070,10 +1070,8 @@ func Append(slice, data[]byte) []byte {
if l + len(data) > cap(slice) { // reallocate if l + len(data) > cap(slice) { // reallocate
// Allocate double what's needed, for future growth. // Allocate double what's needed, for future growth.
newSlice := make([]byte, (l+len(data))*2) newSlice := make([]byte, (l+len(data))*2)
// Copy data (could use bytes.Copy()). // The copy function is predeclared and works for any slice type.
for i, c := range slice { copy(newSlice, slice)
newSlice[i] = c
}
slice = newSlice slice = newSlice
} }
slice = slice[0:l+len(data)] slice = slice[0:l+len(data)]