Skip to content

Instantly share code, notes, and snippets.

@tebeka
Created September 15, 2022 14:44
Show Gist options
  • Save tebeka/6bbc8b53626074a9cfebe49c3713d97d to your computer and use it in GitHub Desktop.
Save tebeka/6bbc8b53626074a9cfebe49c3713d97d to your computer and use it in GitHub Desktop.
func appendInt(s []int, val int) []int {
idx := len(s)
if len(s) < cap(s) {
// Enough space, use same underlying array
s = s[:len(s)+1]
} else {
// Not enough space, allocate & copy
s1 := make([]int, 2*len(s)+1)
copy(s1, s)
s = s1[:len(s)+1]
}
s[idx] = val
return s
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment