34

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.

icza
  • 389,944
  • 63
  • 907
  • 827
r123454321
  • 3,323
  • 11
  • 44
  • 63
  • 3
    You might find the following article useful: https://blog.golang.org/slices – kostya Apr 18 '16 at 01:20
  • 16
    For your last question: capacity, is the number of elements between the first element that a slice can access and the last element of the underlying array. First element that slice `d` can access is 2nd element in the underlying array, hence capacity is 3. – Akavall Apr 18 '16 at 03:18
  • 1
    Possible duplicate of [Decreasing slice capacity](http://stackoverflow.com/questions/43294449/decreasing-slice-capacity) – Alex Efimov Apr 11 '17 at 13:25
  • And how undelying array for `d` look? – Hrvoje T Dec 30 '18 at 14:05
  • `slice2 = slice1[start:len:cap]`, the change of `len` is reversible, and the change of `cap` is irreversible. – Time Killer Jun 08 '20 at 05:45

2 Answers2

23

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'

icza
  • 389,944
  • 63
  • 907
  • 827
1

The official docs mentions that.

The length of a slice is the number of elements it contains.

The capacity of a slice is the number of elements in the underlying array, counting from the first element in the slice.

So for your 1st q:

Why does c look like [0,0] and have length 2?

Your slice c was created with b[:2] which means it has elements from 0th to 1st position of the slice b. Hence it has only 2 elements so length is 2.

So does setting c to b[:2] zero out the first two elements?(since b was []`)

Now as @icza mentioned go initializes them to 0 by default. By setting c:=b[:2] you initialized b to have 2 elements out of its capacity 5.

For your 2nd q:

why is the capacity of d 3

As one of the commenters @Akavall mentioned.

capacity, is the number of elements between the first element that a slice can access and the last element of the underlying array.

So in your case the 1st element your slice d can access is the 2nd possition element of the array and the last element of underlying array is position 5th. So 5-2 is 3.

I have hereby attached the official diagrammatic representation of slice accessing the array. enter image description here

More examples of initialization and creation of slices are available further in official documentation. https://tour.golang.org/moretypes/13

Ja8zyjits
  • 1,433
  • 16
  • 31