Summary: in this tutorial, you’ll learn about the Go slice and how to use it effectively.
In Go, slices are like arrays but have a dynamic size. By definition, a slice is a dynamically sized, flexible view into elements of an array.
Declaring a slice
To declare a slice, you use the var
keyword, slice name, square brackets, and the type of the elements:
var sliceName [] type
Code language: Go (go)
This syntax is similar to declaring an array except that it does not have a fixed size. Therefore, we don’t have to specify the size in the square brackets. For example:
var names [] string
Code language: Go (go)
This example declares the names
slice that will store a list of strings.
Initializing a slice
You can declare and initialize a slice in a single line of code like this:
var names [] string = [] string {"Alice", "Bob", "John"}
Code language: Go (go)
Inside a function, you can use the short variable declaration to declare and initialize a slice:
names := [] string {"Alice", "Bob", "John"}
Code language: Go (go)
Typically, you create a slice from an array. For example:
package main
import "fmt"
func main() {
guests := [3] string{"Alice", "Bob", "John"}
vip := guests[0:2]
for index, name := range vip {
fmt.Println(index, name)
}
}
Code language: Go (go)
Output:
0 Alice
1 Bob
Code language: Go (go)
In this example, we create a slice vip
from the guests
array. The vip
slice includes the first and second elements of the guests
array.
The for range iterates over elements of the vip
slice and displays their indexes and values on the screen.
Modifying the slice elements
A slice is a reference to an underlying array. When you change an element in a slice, the corresponding element of the underlying array also changes. For example:
package main
import "fmt"
func main() {
guests := [3] string {"Alice", "Bob", "John"}
vip := guests[0:2]
vip[0] = "Linda"
fmt.Println(guests)
}
Code language: Go (go)
Output:
[Linda Bob John]
Code language: Go (go)
In this example, we change the first element of the slice vip
to Linda, the first element in the guests
array also changes.
Length and capacity
Slices have two properties:
- Length stores the number of elements in the slice.
- Capacity is the number of elements in the underlying array from the index where you create the slice.
For example:
package main
import "fmt"
func main() {
scores := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
avergeScores := scores[1:4]
fmt.Println("Length of slice:",len(avergeScores))
fmt.Println("Capacity of slice:",cap(avergeScores))
}
Code language: Go (go)
Output:
Length of slice: 3
Capacity of slice: 9
Code language: Go (go)
In this example:
- The
scores
array has 10 numbers from 1 to 10. - The length of the
averageScores
slice is 3 because it includes elements from the index 1 to 3 (1, 2, and 3). - The capacity of the
averageScores
slice is 9 because it contains elements from index 1 to the end of the array (2, 3, … 10).
Appending an element to a slice
To append an element to a slice, you use the built-in append()
function. The append()
function adds an element at the end of the slice and returns a new slice:
package main
import "fmt"
func main() {
names := [] string {}
names = append(names, "Alice")
names = append(names, "Bob")
names = append(names, "Joe")
for index, name := range names {
fmt.Println(index, name)
}
}
Code language: Go (go)
Output:
0 Alice
1 Bob
2 Joe
Code language: Go (go)
Summary
- A slice is a view into an underlying array and provides a more flexible interface.
- Use the
array[start:end]
syntax to create a slice from an array. - Use the
len
function to get the number of elements in a slice. - Use the
cap
function to get the capacity of a slice.