Skip to content

Instantly share code, notes, and snippets.

@l1905
Created March 23, 2020 01:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l1905/424f15d629ee0dcdb5c4727c0c83559a to your computer and use it in GitHub Desktop.
Save l1905/424f15d629ee0dcdb5c4727c0c83559a to your computer and use it in GitHub Desktop.
func AppendByte(slice []byte, data ...byte) []byte {
m := len(slice)
n := m + len(data)
if n > cap(slice) { // if necessary, reallocate
// allocate double what's needed, for future growth.
newSlice := make([]byte, (n+1)*2)
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0:n]
copy(slice[m:n], data)
return slice
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment