I'm going through the Go-tour and this module has got me thinking, it seems that every-time you modify the view by slicing the lower bound, the capacity and length of the slice is reduced. However as taught earlier, the underlying array created by the slice does not get altered.
I simplified the example code down to this:
package main
import "fmt"
func main() {
s := []int{2, 3, 5, 7, 11, 13}
s = s[6:]
fmt.Println(cap(s), len(s), s)
}
This prints: 0 0 []
meaning this slice is now totally useless.
However, the array is un-altered, the array is still (for the sake of visualization):
[2, 3, 5, 7, 11, 13]
and referenced by s
, meaning it won't get garbage collected.
So my question is, is this a side effect of slices or is this expected / preferred behavior? and secondly, is there a way to restore the view back to the original? (showing [2, 3, 5, 7, 11, 13]
)