简体   繁体   中英

Go slices - capacity/length?

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.

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'

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. 在此处输入图片说明

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM