Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decreasing slice capacity

Tags:

slice

go

My question is about slice length and capacity. I'm learning about Go here: https://tour.golang.org/moretypes/11.

(My question was marked as a possible duplicate of this; however, this is not the case. My question is specifically about the cutting off the first few elements of a slice and the implications of that.)

Why does the line s = s[2:] decrease the capacity when s = s[:4] and s = s[:0] do not? The only difference I see is that there is a number before the colon in s = s[2:] while there is a number after the colon in the other two lines.

Is there any way to recover the first two elements that we cut off with s = s[2:]?

package main  import "fmt"  func main() {     s := []int{2, 3, 5, 7, 11, 13}     printSlice(s)      // Slice the slice to give it zero length.     s = s[:0]     printSlice(s)      // Extend its length.     s = s[:4]     printSlice(s)      // Drop its first two values.     s = s[2:]     printSlice(s) }  func printSlice(s []int) {     fmt.Printf("len=%d cap=%d %v\n", len(s), cap(s), s) } 

After clicking the Run button, we get the following.

len=6 cap=6 [2 3 5 7 11 13] len=0 cap=6 [] len=4 cap=6 [2 3 5 7] len=2 cap=4 [5 7] 
like image 362
Pizzas Avatar asked Apr 08 '17 13:04

Pizzas


People also ask

How can I increase my slice capacity?

The length and capacity of a slice s can be obtained using the expressions len(s) and cap(s) . You can extend a slice's length by re-slicing it, provided it has sufficient capacity. Try changing one of the slice operations in the example program to extend it beyond its capacity and see what happens.

What is the use of capacity in slice in Golang?

In Go, the length of a slice tells you how many elements it contains. It can be obtained using the len() function. The capacity is the size of the slice's underlying array and can be obtained with the cap() function.

What is the default capacity of slice in Golang?

Construction. The default zero value of a slice is nil . The functions len , cap and append all regard nil as an empty slice with 0 capacity.

How do you determine the size of a slice?

To get length of slice in Go programming, call len() function and pass the slice as argument to it. len() function returns an integer, the length of slice. The function returns an integer value, representing the length of given slice.


2 Answers

You can read more about slices here. But I think this passage answers your question:

Slicing does not copy the slice's data. It creates a new slice value that points to the original array. This makes slice operations as efficient as manipulating array indices. Therefore, modifying the elements (not the slice itself) of a re-slice modifies the elements of the original slice.

So you cannot recover the slice data if you are assigning it to the same variable.

The capacity decrease is because by dropping the first 2 elements you are changing the pointer to the new slice (slices are referenced by the pointer to the first element).

How slices are represented in the memory: def

make([]byte, 5) 

memory vis

s = s[2:4] 

slice cap decrease

like image 66
Alex Efimov Avatar answered Oct 04 '22 18:10

Alex Efimov


You can use a full slice expression:

package main  func main() {    s := []int{2, 3, 5, 7, 11, 13}    { // example 1       t := s[:0]       println(cap(t) == 6)    }    { // example 2       t := s[:0:0]       println(cap(t) == 0)    } } 

https://golang.org/ref/spec#Slice_expressions

like image 44
Zombo Avatar answered Oct 04 '22 17:10

Zombo