Hacker News new | past | comments | ask | show | jobs | submit login

I'm not sure I'm following the preamble about the nuances of a slice with a zero capacity allocating a new backing array, given the fact that if I follow the link to the docs and then to the source, the implementation is exactly how I would have expected it to be done: append(S([]E{}), s...) - which of course is different and would make the preamble redundant.



My guess is that originally, the post didn't have "// body omitted" below.

The actual implementation requires having the `S` type to refer to, and the point of this post is to explain why the `S` type has to be named. By writing it as they did, the "// body omitted" one _could_ have had the same body, even without an 'S' type to refer to.

I bet the "// body omitted" bit of the post got refactored, and the reason for making the first one different from the stdlib impl got lost.


The generic function is cloning a slice so you need to allocate a new backing array. (think deep-copy)

Here that's just a one-liner that works.

I need to check but I think that if you need to keep the same capacity, you may want to preallocate manually.


I understand that, my point solely rest around defining the clone function from the slices package in the article, explaining why, yet the actual implementation is different. The source code I found via the docs doesn't consider capacity, it merely uses a empty construct and appends.


Ah you're right... It's rather a shallow copy.

Good point. I have no clue either.

Maybe you can open an issue?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: