--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/c2bea525-0636-4365-b551-59d1fceb5959%40googlegroups.com.
AFAIK it is allowed to re-slice past the len but it's rather not the way slices should be ever used.
You missed the point about slices, I guess.
Many slices can refer to the same internal buffer, giving you sort of window view of the data, thus as long as any slice still refer to the internal buffer GC won't collect it.
On Thursday, March 26, 2020 at 6:39:50 PM UTC+1, robfig wrote:Reducing a slice's length makes the elements unreachable, but the GC appears to still treat them as live, based on this snippet:I would have expected the HeapInUse to go down in the second measurement.Why is that?I presume that the GC is traversing the slice's capacity rather than length, but I'm not sure why this is necessary. Is there a way (without using unsafe) to access slice elements past the length?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/27be27b0-5b55-4035-9953-072809fc7932%40googlegroups.com.
I disagree. I do that all the time. It's also how `append` was implemented before it existed as a predeclared function. It's also, FWIW, used in bytes.Buffer. I agree that unless your API is very clear about it, you shouldn't really access a slice past the length for one of your arguments. But there *are* APIs where it's clear that's happening (e.g. anything having "append" in it's doc-string) and there are use-cases where you control the slice for its entire lifetime and where it can make code more readable.