In the general case however, it's not any more efficient than the two step way, though append could be special cased not to need the extra work.
True. I wouldn't suggest to add this feature in general case. This feature is only valid in append().
How is that a waste of memory and CPU?
Consider that each call to append() will alloc a new array and copy old data to it, if src slice contains tons of data, it's a big waste.
From my point of view, append() is just a syntactic candy. In my case, it can save time on coding but waste time on running. I'd rather choose to waste my coding time.
I can write a "perfect" solution, but I cannot make it a func for reuse. I have to repeat, repeat, repeat myself in similar scenarios. It's wired. Why not make the candy tasty? :(
// a "perfect" solution
// this code is to append slice1 and slice2 to slice3
// all slices are []int type
len1, len2, len3 := len(slice1), len(slice2), len(slice3)
if cap(slice3) >= len1 + len2 + len3 {
slice3 := slice3[:len1 + len2 + len3]
} else {
s := make([]int, len1 + len2 + len3)
copy(s, slice3)
slice3 = s
}
copy(slice3[len3:len1 + len3], slice1)
copy(slice3[len1 + len3:], slice2)