Lesson 12Accessing Slices

The data type of an array includes the size of the array. This means that once you have created an array, you cannot change its size. In some cases, though, having fixed-size arrays can be limiting.

Go supports variable-sized data structures through the use of slices. Unlike arrays, the slice data type does not depend on its length, so a slice is a dynamic and powerful tool, with more flexibility than an array. Essentially, a slice is a subset of values in an array. In this lesson, we will dive into slices to understand how they work.

HOW SLICES WORK

A slice is an abstraction built on top of the array type. This means that it allows you to use the same logic you would with an array, but with more powerful features and flexibility.

A slice is a subset of an array. In other words, you can formally create an array and then create a slice that references only some elements of that array. Because each element of an array must be the same data type, it follows that each element of a slice must also be the same data type.

A slice consists of a pointer to an element in an array, the length of the segment, and its capacity (the maximum number of elements the slice can hold, which depends on the underlying array). To understand ...

Get Job Ready Go now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.