Created
February 19, 2016 00:20
-
-
Save tjs-w/8d41c5f5d1960c9ec818 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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