Skip to content

Instantly share code, notes, and snippets.

@tjs-w
Created February 19, 2016 00:20
Show Gist options
  • Save tjs-w/8d41c5f5d1960c9ec818 to your computer and use it in GitHub Desktop.
Save tjs-w/8d41c5f5d1960c9ec818 to your computer and use it in GitHub Desktop.
func Append(slice, data []byte) []byte {
l := len(slice)
if l + len(data) > cap(slice) { // reallocate
// Allocate double what's needed, for future growth.
newSlice := make([]byte, (l+len(data))*2)
// The copy function is predeclared and works for any slice type.
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0:l+len(data)]
for i, c := range data {
slice[l+i] = c
}
return slice
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment