Slices in Go are similar to arrays but more dynamic and versatile. While both store multiple values of the same data type in a single variable, slices differ because their lengths can grow or shrink as needed, unlike arrays with fixed lengths.
Creating a Slice
There are three primary ways to create a slice in Go:
- Using the
[]datatype{values}
format - Creating a slice from an array
- Using the
make()
function
Creating a Slice Using []datatype{values}
To declare a slice with this method:
Syntax:
sliceName := []datatype{values}
For instance:
motorcycleModels := []string{}
The code above creates an empty slice with both a length and capacity of 0.
To initialize a slice during its declaration:
motorcycleModels := []string{"Ducati", "Yamaha", "Honda"}
This example declares a slice with a length and capacity of 3.
Go provides two essential functions for slice handling:
len()
returns the number of elements in the slice.cap()
returns the capacity of the slice, i.e., the maximum number of elements it can grow to.
Example:
package main import ("fmt") func main() { emptySlice := []string{} fmt.Println(len(emptySlice)) // Length fmt.Println(cap(emptySlice)) // Capacity fmt.Println(emptySlice) initializedSlice := []string{"Suzuki", "Honda", "Yamaha", "Ducati"} fmt.Println(len(initializedSlice)) fmt.Println(cap(initializedSlice)) fmt.Println(initializedSlice) }
Output:
0 0 [] 4 4 [Suzuki Honda Yamaha Ducati]
Here, the first slice is empty, so its length and capacity are 0
. The second slice includes elements, so the length and capacity correspond to the number of those elements.
Creating a Slice from an Array
Slices can be derived from arrays by specifying a range of elements.
Syntax:
var arrayName = [length]datatype{values} // Declare an array sliceName := arrayName[start:end] // Slice from the array
Example:
package main import ("fmt") func main() { motorcycleYears := [6]int{2019, 2020, 2021, 2022, 2023, 2024} recentYears := motorcycleYears[2:4] fmt.Printf("recentYears = %v\n", recentYears) fmt.Printf("length = %d\n", len(recentYears)) fmt.Printf("capacity = %d\n", cap(recentYears)) }
Output:
recentYears = [2021 2022] length = 2 capacity = 4
In this case:
recentYears
is a slice of length2
, derived from the arraymotorcycleYears
.- It starts from the third element (index
2
) and ends before the fifth element (index4
). - The slice’s capacity is
4
because it can grow from its starting point to the end of the array.
Creating a Slice Using the make()
Function
The make()
function is a powerful tool for creating slices with pre-defined lengths and capacities.
Syntax:
sliceName := make([]type, length, capacity)
Note: If the capacity
parameter is omitted, it defaults to the length
.
Example:
package main import ("fmt") func main() { sliceWithCapacity := make([]int, 5, 10) fmt.Printf("sliceWithCapacity = %v\n", sliceWithCapacity) fmt.Printf("length = %d\n", len(sliceWithCapacity)) fmt.Printf("capacity = %d\n", cap(sliceWithCapacity)) sliceWithoutCapacity := make([]int, 5) fmt.Printf("sliceWithoutCapacity = %v\n", sliceWithoutCapacity) fmt.Printf("length = %d\n", len(sliceWithoutCapacity)) fmt.Printf("capacity = %d\n", cap(sliceWithoutCapacity)) }
Output:
sliceWithCapacity = [0 0 0 0 0] length = 5 capacity = 10 sliceWithoutCapacity = [0 0 0 0 0] length = 5 capacity = 5
Here:
- The first slice has a length of
5
and capacity of10
. - The second slice’s length and capacity are both
5
, as the capacity parameter is omitted.