Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

golang slice, slicing a slice with slice[a:b:c]

Tags:

slice

go

I read go slice usage and internals and Slice and Effective go#slice but there is nothing about slicing a slice with 3 number like this : slice[a:b:c]

For example this code :

package main  import "fmt"  func main() {     var s = []string{"a", "b", "c", "d", "e", "f", "g"}     fmt.Println(s[1:2:6], len(s[1:2:6]), cap(s[1:2:6]))     fmt.Println(s[1:2:5], len(s[1:2:5]), cap(s[1:2:5]))     fmt.Println(s[1:2], len(s[1:2]), cap(s[1:2]))  } 

go playground result is this :

[b] 1 5 [b] 1 4 [b] 1 6 

I can understand that the third one is something about capacity, but what is the exact meaning of this?
Do I miss something in documents?

like image 710
fzerorubigd Avatar asked Jan 14 '15 07:01

fzerorubigd


People also ask

How do you slice slices in Golang?

Go. Using make() function: You can also create a slice using the make() function which is provided by the go library. This function takes three parameters, i.e, type, length, and capacity. Here, capacity value is optional.

Does Golang pass slice by reference?

When we pass a slice to a function as an argument the values of the slice are passed by reference (since we pass a copy of the pointer), but all the metadata describing the slice itself are just copies.

How do you read a slice in Golang?

In Go, there are two functions that can be used to return the length and capacity of a slice: len() function - returns the length of the slice (the number of elements in the slice) cap() function - returns the capacity of the slice (the number of elements the slice can grow or shrink to)

How do I declare a slice in Golang?

A slice can be declare using new keyword followed by capacity in square brackets then type of elements the slice will hold.


2 Answers

The syntax has been introduced in Go 1.2, as I mentioned in "Re-slicing slices in Golang".
It is documented in Full slice expressions:

a[low : high : max] 

constructs a slice of the same type, and with the same length and elements as the simple slice expression a[low : high].
Additionally, it controls the resulting slice's capacity by setting it to max - low.
Only the first index may be omitted; it defaults to 0.

After slicing the array a:

a := [5]int{1, 2, 3, 4, 5} t := a[1:3:5] 

the slice t has type []int, length 2, capacity 4, and elements

t[0] == 2 t[1] == 3 

The design document for that feature had the following justification:

It would occasionally be useful, for example in custom []byte allocation managers, to be able to hand a slice to a caller and know that the caller cannot edit values beyond a given subrange of the true array.

The addition of append to the language made this somewhat more important, because append lets programmers overwrite entries between len and cap without realizing it or even mentioning cap.

like image 194
VonC Avatar answered Sep 18 '22 08:09

VonC


In a slice expression slice[a:b:c] or aSlice[1:3:5]

a:b or 1:3 -> gives length a:c or 1:5 -> gives capacity 

We can extract both length and capacity from a slice expression with 3 numbers/indices, without looking at the source slice/array.

expression| aSlice[low:high:max]  or aSlice[a:b:c]    or aSlice[1:3:7] ------------------------------------------------------------------------ Length    | len(aSlice[low:high]) or len(aSlice[a:b]) or len(aSlice[1:3]) Capacity  | len(aSlice[low:max])  or len(aSlice[a:c]) or len(aSlice[1:7]) ------------------------------------------------------------------------ 

Read more here at Slice Expressions

Playground

like image 31
Robert Ranjan Avatar answered Sep 22 '22 08:09

Robert Ranjan