Go Slices
Slices are one of the most powerful and commonly used features in Go. They provide a flexible and dynamic way to work with collections of data—making them far more versatile than arrays.
What Are Slices in Go?
A slice is a dynamically-sized, flexible view into the elements of an array. Like arrays, slices store multiple values of the same type—but unlike arrays, their size can grow or shrink as needed.
Key Benefits of Slices
- Dynamic size (can expand and shrink)
- Built-in functions for easy manipulation
- More commonly used than arrays in real-world Go programs
Ways to Create Slices in Go
There are three primary ways to create slices:
-
Using slice literals (
[]type{values}) - Creating a slice from an array
- Using the
make()function
1. Creating a Slice Using Slice Literals
Syntax: sliceName := []datatype{values}
Explanation:
- An empty slice has both length and capacity equal to 0.
- When initialized with values, both length and capacity match the number of elements.
Understanding len() and cap()
-
len(slice)→ Returns the number of elements in the slice -
cap(slice)→ Returns the maximum number of elements the slice can hold before resizing
2. Creating a Slice from an Array
Slices can be derived from arrays using index ranges.
Syntax: slice := array[start:end]
Explanation:
- The slice starts at index 1 and ends before index 4.
- Capacity is calculated from the start index to the end of the underlying array.
- Since the array has 6 elements, capacity = 6 - 1 = 5.
3. Creating a Slice Using make()
The make() function allows you to create slices with a
predefined length and capacity.
Syntax: slice := make([]type, length, capacity)
If capacity is omitted, it defaults to the length.
Explanation:
-
bufferhas space to grow up to 6 elements. -
queuehas capacity equal to its length because capacity wasn’t specified.
When Should You Use Slices?
Use slices when:
- You need a dynamic collection of elements
- The number of elements is not known in advance
- You want efficient and flexible data handling
Accessing and Modifying Slices in Go
Slices in Go are flexible and powerful, allowing you not only to store collections of data but also to efficiently access, modify, and expand them. This section covers essential slice operations every Go developer should understand.
Access Elements of a Slice
You can access elements in a slice using their index position. Like arrays, slice indexing starts at 0.
Key Point: Indexing starts at 0, so
scores[0] refers to the first element.
Modify Elements in a Slice
Slices are mutable, meaning you can update their values directly using index positions.
Append Elements to a Slice
You can dynamically grow a slice using the built-in
append() function.
Syntax: slice = append(slice, elements...)
Note: When capacity is exceeded, Go automatically allocates a new underlying array (usually doubling capacity).
Append One Slice to Another
You can merge two slices using append() along with the
... operator.
Changing the Length of a Slice
Unlike arrays, slices can change size dynamically through re-slicing or appending.
Memory Efficiency with Slices
Slices internally reference an underlying array. If that array is large but
you only need a small portion, it can lead to unnecessary memory usage. To
avoid this, use the copy() function to create a smaller,
independent slice.
Using the copy() Function
Syntax: copy(destination, source)
- Copies elements from source to destination
- Returns the number of elements copied
Why This Matters:
- Prevents holding references to large unused arrays
- Improves memory efficiency in large-scale applications
