7 ms·
> There's also no way to shrink the memory underlying a slice. Sorry, that is incorrect: https://pkg.go.dev/slices#Clip https://pkg.go.dev/slices#Clip > It's
by RVuRnvbM2e 10mo ago
> There's also no way to shrink the memory underlying a slice.
Sorry, that is incorrect: https://pkg.go.dev/slices#Clip https://pkg.go.dev/slices#Clip
> It's a common newbie mistake to think they do work like that, and write "append(s, ...)" instead of "s = append(s, ...)". It might even randomly work a lot of the time.
"append(s, ...)" without the assignment doesn't even compile. So your entire post seems like a strawman?
https://go.dev/play/p/icdOMl8A9ja https://go.dev/play/p/icdOMl8A9ja
> So (generalizing) Go won't implement a feature that makes mistakes harder, if it makes the language more complicated
No, I think it is more that the compromise of complicating the language that is always made when adding features is carefully weighed in Go. Less so in other languages.
- knorker 10mo agoDoes clipping make the rest eligible for GC? Clipping doesn't seem to automatically move the data, so while it does mean appending will reallocate, it doesn't actually shrink the underlying array, right?
- wrs 10mo agoYes, my example was garbled. Thanks @masklinn for correcting it for me below!