-
-
Save tebeka/6bbc8b53626074a9cfebe49c3713d97d to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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