Chapter 6

Slicing and Dicing Using Arrays and Slices

IN THIS CHAPTER

Bullet Understanding arrays and slices

Bullet 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.

Schematic illustration of an array which is a collection of items of the same type.

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 ...

Get Go Programming Language For Dummies 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.