Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go slices - capacity/length?

Tags:

Trying to learn Go from the tutorial right now, and have a pretty basic question:

 func main() {   a := make([]int, 5)   // [0,0,0,0,0] len=5 cap=5    b := make([]int, 0, 5)   // [] len=0 cap=5    c := b[:2]   // [0,0] len=2 cap=5    d := c[2:5]   // [0,0,0] len=3 cap=3 } 

Why does c look like [0,0] and have length 2? b was not originally zero'ed hence it being []. So does setting c to b[:2] zero out the first two elements?

Also, why is the capacity of d 3? Very confused.

Thanks in advance.

like image 327
r123454321 Avatar asked Apr 18 '16 00:04

r123454321


People also ask

How do you find the length of a slice in Go?

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.

What is the maximum capacity of slice in Golang?

The length of the slice is 5, which means the total number of elements present in the slice is 5 and the capacity of the slice 6 means it can store a maximum of 6 elements in it.

What is the difference between length and capacity 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.


1 Answers

All your variables have a slice type. Slices have a backing array. In Go you can't access uninitialized variables. If you don't explicitly provide a value when you create a new variable, they will be initialized with the zero value of the variable's type.

This means when you create a slice with make([]int, 0, 5), it also creates a backing array, the backing array will be initialized with its zero value, and this zeroed array will be sliced. The zero value of an array type is an array having the zero value of element type for each of its elements.

So even though you didn't explicitly set every elements of the backing array to 0, they will be zeroed automatically. So when you do c := b[:2], it will slice the b slice, and c will have a length of 2, and those 2 elements will be 0.

And when you do d := c[2:5] that slices the c slice, its length will be 5-2 = 3, and its capacity will also be 5-2 = 3 because slicing a slice will result in a new slice which shares the same backing array, and the capacity will be the first element till the last of the backing array (unless you use a full slice expression which also controls the resulting slice's capacity).

Must-read blog posts for newcomers who want to understand slices and arrays:

The Go Blog: Go Slices: usage and internals

The Go Blog: Arrays, slices (and strings): The mechanics of 'append'

like image 157
icza Avatar answered Oct 12 '22 22:10

icza