Go Slices
Go slices provide a powerful and flexible way to work with sequences of data. They are dynamically sized, meaning they can grow or shrink as needed, unlike arrays which have a fixed size. Understanding slices is crucial for writing efficient and idiomatic Go code.
What are Slices?
A slice is a dynamic view into an underlying array. It’s composed of three components:
- Pointer: Points to the first element in the slice within the underlying array.
- Length: The number of elements in the slice.
- Capacity: The number of elements from the start of the slice to the end of the underlying array.
Creating Slices:
There are several ways to create slices:
- Using the
make
function:make([]T, length, capacity)
creates a slice of typeT
with the specified length and capacity. If capacity is omitted, it defaults to the length.
numbers := make([]int, 5, 10) // Slice of ints, length 5, capacity 10
- Slice literal: Similar to array literals, but without specifying the size.
names := []string{"Alice", "Bob", "Charlie"}
- Slicing an existing array or slice:
array[start:end]
creates a slice fromarray
starting at indexstart
up to (but not including) indexend
.
arr := [5]int{1, 2, 3, 4, 5}
slice := arr[1:4] // Slice contains {2, 3, 4}
Key Operations:
append
: Adds elements to the end of a slice, increasing its length. If the capacity is reached, a new underlying array is allocated, and the data is copied.
numbers := []int{1, 2, 3}
numbers = append(numbers, 4, 5) // numbers is now {1, 2, 3, 4, 5}
len
: Returns the length of the slice.
count := len(numbers) // count is 5
cap
: Returns the capacity of the slice.
capacity := cap(numbers) // Capacity may be greater than length
copy
: Copies elements from one slice to another.
source := []int{1, 2, 3}
destination := make([]int, 3)
copy(destination, source) // destination is now {1, 2, 3}
Important Considerations:
- Slices are reference types: Modifying a slice may modify the underlying array, affecting other slices that share the same underlying array.
- Nil slices: A nil slice has a length and capacity of zero and no underlying array. It’s often used as the initial value for a slice.
- Empty slices: An empty slice has a length of zero but may have a non-zero capacity and an underlying array. It’s created using
make([]T, 0)
or[]T{}
.
By understanding these concepts and operations, you can effectively utilize slices to manage collections of data in your Go programs, leading to more efficient and dynamic code.