Chapter 6
Slicing and Dicing Using Arrays and Slices
IN THIS CHAPTER
Understanding arrays and slices
Working with arrays and slices
In Chapter 2, I show you how to declare variables and constants to use the various basic data types in Go — int, bool, float32, and so on. In real-life applications, you usually need to deal with collections of data. For example, you may need to store the temperature of a city for the past 30 days, so you need to store a collection of 30 floating-point numbers. This is where array comes in.
In this chapter, I explain how to use arrays to store collections of items. I also introduce you to another related data structure: slices. Together, arrays and slices provide a very flexible way to manipulate collections of data.
Arming Yourself to Use Arrays
In Go, an array is a numbered sequence of items of a specific length. Think of arrays as collections of items of the same type, such as the ones shown in Figure 6-1.
FIGURE 6-1: An array is a collection of items of the same type.
The values an array holds are known as its items or elements. In Go, all the elements in an array must be of the same type. After an array is created, the array can't change in ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access