Go Slices

Go Slices

Kishore V


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}

numbers: [] Length: 0 Capacity: 0 fruits: [Apple Banana Cherry] Length: 3 Capacity: 3

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]

Selected grades: [90 78 92] Length: 3 Capacity: 5

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.

buffer: [0 0 0] Length: 3 Capacity: 6 queue: [0 0 0 0] Length: 4 Capacity: 4

Explanation:

  • buffer has space to grow up to 6 elements.
  • queue has 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.

First score: 85 Last score: 92

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.

Updated temperatures: [28 35 32]

Append Elements to a Slice

You can dynamically grow a slice using the built-in append() function.
Syntax: slice = append(slice, elements...)

Before append: [101 102 103] Length: 3 Capacity: 3 After append: [101 102 103 104 105] Length: 5 Capacity: 6

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.

Combined slice: [2 4 6 1 3 5] Length: 6 Capacity: 6

Changing the Length of a Slice

Unlike arrays, slices can change size dynamically through re-slicing or appending.

Initial: [30 40 50 60] Len: 4 Cap: 5 Resliced: [30 40] Len: 2 Cap: 5 Expanded: [30 40 80 90 100] Len: 5 Cap: 10

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
Original: [5 10 15 20 25 30 35 40] Length: 8 Capacity: 8 Optimized copy: [5 10 15] Length: 3 Capacity: 3

Why This Matters:

  • Prevents holding references to large unused arrays
  • Improves memory efficiency in large-scale applications


Our website uses cookies to enhance your experience. Learn More
Accept !