liveBook · Manning

Chapter 4: Collection types

What you will learn in chapter 4 of Go in Action, Second Edition

Introduces Go’s built-in collection types and the kinds of data each is suited to holding.

After this chapter you will be able to

What it builds on

Section by section

Introduction

Introduces Go’s built-in collection types and the kinds of data each is suited to holding.

Key terms: arrays, slices, fixed-size, dynamic, maps, key-value data, built into the language

4.1 Arrays

Introduces arrays as fixed-length, contiguous collections and explains how indexing and memory layout affect access and iteration.

Key terms: array, fixed-length, contiguous, index, CPU caches, index arithmetic, pointer arithmetic

4.1.1 Declaring arrays

This section explains how to declare Go arrays, initialize their elements, and choose between explicit and compiler-inferred lengths.

Key terms: array length, element type, array type, var, short variable declaration, array literal, zero value, indices, automatic array length, …

4.1.2 Array type and length

This section explains that an array’s length is part of its type and that len reports its fixed size.

Key terms: array length, array type, [5]int, [4]int, len(), int, array size

4.1.3 Working with array elements

This section shows how to read and change array elements by index, including elements that hold pointers.

Key terms: array elements, square bracket notation, index, pointer, indirection operator, array of pointers

4.1.4 Iterating over arrays with for

This section shows how to traverse arrays with range or index-based for loops, including how to modify elements and control traversal order.

Key terms: range clause, index, blank identifier (_), range, copy, pointers, for loop, len, indices

4.1.5 Arrays as values

This section explains that assigning an array copies the entire array, including its elements, while copied pointers can still refer to the same underlying objects.

Key terms: array value, same type, array assignment, pointer types, pointer values, same memory

4.1.6 Multidimensional arrays

This section shows how Go represents multidimensional data with nested arrays and how indexing, copying, and type inspection work with them.

Key terms: multidimensional arrays, arrays of arrays, element type, inner values, subarrays, values, same element type, lengths, %T, fmt.Printf

4.1.7 Passing arrays to functions

This section explains why passing large arrays to functions can be costly and how passing a pointer avoids copying the array while allowing changes to affect the original.

Key terms: passed by value, array, copy, pointer to an array, processPixels, original value

4.1.8 The problem with arrays

This section explains why fixed-size, copied-by-value arrays are often awkward for lists and introduces slices as the solution.

Key terms: arrays, fixed number of items, slices, copied

4.2 Slices

The section introduces slices as flexible, ordered collections and explains how a slice header provides a dynamic view of an underlying array.

Key terms: backing array, slice header, length, capacity, slices, arrays, dynamic view

4.2.1 Declaring slices with slice literals

This section shows how a slice literal creates a slice and determines its backing array, length, and capacity.

Key terms: slice literal, backing array, len, cap, length, capacity

4.2.2 Declaring slices with make

This section shows how to create zero-initialized slices with make and how length and capacity determine which allocated elements are accessible.

Key terms: make, length, zero value, backing array, capacity, append, runtime panic, length 0

4.2.3 Nil slices

This subsection explains what a nil slice is, what operations work on it, and how to give it values before accessing elements.

Key terms: nil slice, backing array, len, cap, runtime panic, nil slices, placeholders, literals, make, append

4.2.4 Growing slices with append

This section shows how to grow slices with append, including when it reuses the backing array and when it allocates a new one.

Key terms: append, nil slice, ... operator, length, capacity, backing array, slice header, array doubling, amortized doubling

4.2.5 Avoiding append surprises

This section explains why appending inside a function can make changes invisible to the caller and how to avoid that surprise.

Key terms: append, backing array, slice, capacity, length, return the appended slice

4.2.6 Slice expressions

This section shows how slice expressions create shared-storage windows into slices or arrays, how a third index limits capacity, and how to pass a subset to a function.

Key terms: slice expression, low, high, backing array, slice expressions with capacity, a[low : high : max], capacity, max, range, len

4.2.7 Copying slices with copy

This section explains how to use Go’s copy function to duplicate slice elements without relying on append or shared slice expressions.

Key terms: copy, src, dst, length, append, slice expression, make, destination slice

4.2.8 Avoiding runtime panics when accessing slice values

This section explains why indexing a slice can panic at runtime and compares length checks, range loops, and deliberate panics as ways to handle slices of unexpected size.

Key terms: slice, index, runtime panic, out-of-bounds, len, length check, error, range loop, index access, panic

4.2.9 Nil slices versus empty slices

This section explains why a zero-length slice is not necessarily nil and how to choose the right emptiness check.

Key terms: nil slice, empty slice, len, backing array, slice header

4.3 Maps

This section introduces maps as unordered key/value collections and explains how hashing makes key-based lookup efficient.

Key terms: map, key, value, unordered collection, key/value pairs, hash table, bucket, hash function, key/value pair, low-order bits (LOBs)

4.3.1 Declaring maps

This section explains how to declare and initialize Go maps, and why nil maps need care before use.

Key terms: map[K]E, key type, element type, make, map literal, var, nil map, == nil, runtime panic

4.3.2 Setting and accessing map values

This section explains how to set and retrieve map values, check whether a key exists, and use zero values for convenient lookups.

Key terms: key, element type, map value, zero value, two-variable assignment, Boolean value, membership list, set

4.3.3 Removing values from maps

4.3.4 Getting the number of values in a map

4.3.5 Iterating over maps

This section shows how to range over a Go map, explains why iteration order varies, and uses iteration with delete to filter entries.

Key terms: range loop, map key, copy of the value, unspecified order, range statement, delete, filtering, concurrent map access

4.3.6 Passing maps to functions

Passing a map to a function lets the function modify map contents that the caller can see.

Key terms: map, reference types, underlying data structure

4.3.7 Key and value types

Map values can have any type, but map keys must be comparable with ==, which allows some structs as keys but rules out slices, maps, functions, and structs containing them.

Key terms: key types, map elements, comparable, == operator, struct type, slice value

4.4 Summary

The summary contrasts Go’s three collection types and highlights how slice growth and shared data affect their use.

Where this is used later

Concepts explained in this chapter

← Chapter 3: Primitive types and operators Chapter 5: Working with types →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.