What is slice
Slice is a dynamic collection of elements with similar types of data.
Unlike arrays, that fixed size, slices can dynamically grow or shrink according to the requirements.
Create slice
1 2 |
slice := []int{10, 20, 30, 40, 50} |
In the above example:
slice
is the name of slice.int
represents types of elements that is integers.{10, 20, 30, 40, 50}
is actual data of slice.
For example:
1 2 3 4 5 6 7 8 9 10 |
package main import "fmt" func main() { slice := []int{1, 2, 3} fmt.Println("Slice: ", slice) } |
Output:
1 2 |
Slice: [1 2 3] |
We can also create a slice using the make()
function. For example,
1 2 |
numbers := make([]int, 5, 7) |
In the above example:
5
is the length of slice.7
is the capacity of the slice.
Components of slice
A slice is made up of three parts – pointer, length and capacity.
Pointer
is a reference to the element of the array that is accessible through the slice.
This reference can be any of elements in the array.
Length
is actual number of elements in the slice.
Capacity
is a total number of elements in the underlying array of the slice.
To understand it better, let’s take a look for the following example:
1 2 3 4 5 6 7 8 9 10 |
package main import "fmt" func main() { slice := []int{1, 2, 3, 4, 5} fmt.Println("Slice: ", slice) } |
Empty slice equals to nil and has length 0:
1 2 3 4 5 6 7 8 9 |
package main import "fmt" func main() { var slice []int fmt.Println("Slice: ", slice) } |
Slicing
Slice also can be created from the given array.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
package main import "fmt" func main() { array := [5]int{1, 2, 3, 4, 5} slice := array[1:4] fmt.Println("Slice: ", slice) fmt.Println("len: ", len(slice)) fmt.Println("cap: ", cap(slice)) } |
Output:
1 2 3 4 |
Slice: [2 3 4] Length: 3 Capacity: 4 |
In the above example slice created by slicing an existing array.
The first index position in a slice is always 0 and the last one will be (length of slice – 1).
Assigning one slice variable to another does not make a copy of the slices contents.
1 2 3 4 5 6 7 8 9 10 11 12 |
package main import "fmt" func main() { a := []int{1, 2, 3, 4, 5} b := a[2:] b[0] = 0 fmt.Println("Slice a:", a) fmt.Println("Slice b:", b) } |
Output:
1 2 3 |
Slice a: [1 2 0 4 5] Slice b: [0 4 5] |
In the above example a
and b
shares the same underlying array. Changes in b
also changed a
even though b
starts at a different offset.
Add elements to slice
Elements can be added to slice in the following way:
1 2 3 4 5 6 7 8 9 10 11 |
package main import "fmt" func main() { slice := []int{1, 2, 3} slice = append(4, 5) fmt.Println("Slice: ", slice) } |
Output:
1 2 |
Slice: [1 2 3 4 5] |
Passing slice to a function
A function that takes slice as argument can change underlying array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
package main import "fmt" func main() { s := []int{10, 20, 30, 40} multiply(s, 2) fmt.Println("Slice:", s) } func multiply(slice []int, factor int) { for i := 0; i < len(slice); i++ { slice[i] = slice[i] * factor } } |
Output:
1 2 |
Slice: [20 40 60 80] |