Stewart Wilcox
Sep 9, 2018 · 1 min read

I think this is incorrect in two ways. Firstly the allocation doesn’t happen each loop, because the capacity is increased by more than one element (for small slices it doubles each time… the number of re-allocations is logarithmic in the final length). Secondly by reassigning the slice as in

result = append(result, item)

the previous array becomes unreachable and can be released. Here is an example:

https://play.golang.org/p/-qp8eQ0CF54

The behavior is similar to that of std::vector in C++ or ArrayList in Java.

    Stewart Wilcox

    Written by

    Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
    Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
    Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade