Overview

4 Collection Types

Go’s collection types exist to make working with multiple values practical, efficient, and expressive. The chapter introduces arrays, slices, and maps as built-in tools for ordered and key-based data, emphasizing that each fills a different role. Arrays provide fixed-size storage with contiguous memory, slices build on arrays to create flexible dynamic lists, and maps offer fast lookup by key instead of by position. Together, these types let programs model real data more naturally than single variables ever could.

Arrays are presented first as value-based, fixed-length collections whose size is part of the type itself. The chapter explains how arrays are declared, initialized, indexed, copied, and iterated, including multidimensional arrays and arrays of pointers. It also highlights important tradeoffs: arrays are efficient for contiguous storage and can be passed by pointer to avoid expensive copying, but their immutability in length makes them awkward for general-purpose collections. This limitation motivates the move to slices, which preserve array-like indexing while adding dynamic length and capacity.

Slices are shown as the preferred ordered collection in Go because they separate the slice header from the backing array, allowing growth, sub-slicing, copying, and efficient sharing of data. The chapter covers slice creation with literals and make, the behavior of nil and empty slices, how append can allocate new backing storage, and why returning the updated slice is often necessary after growth. It then introduces slice expressions and copy for controlled reuse and duplication, before shifting to maps as unordered key/value collections implemented with hash tables. Maps are demonstrated with declaration, access, deletion, iteration, key existence checks, and use with comparable key types such as strings and structs, reinforcing that maps are the most flexible choice when data is best retrieved by meaning rather than position.

Illustation Of intArray In Listing 4.1 After Declaration And Assignment
array types
The intArray Value, After Declaration
declare_array
The intArrayShort Value, After Declaration
declare_array_short
Array Declared With Only Certain Values
declare_array_sparse
Arrays Declared With Lengths Automatically Calculated
declare_array_auto_length
Retrieving And Setting Array Values
array_access_values
An Array Of Pointers That Point To Integers
array_of_pointers
Array Assignment
array_value_assignment
Two Arrays Of Pointers Referencing The Same Objects
array_of_pointers_copy
Two-dimensional Arrays And Their Outer and Inner Values
two_dimensional_arrays
Assigning An Element Of A Two-Dimensional Array
two_dimensional_array_set_value
Values At Different Levels
array_multidimensional_get_values
Passing A Large Array By Value
array_to_function
Slice Internals
slice_internals
Slice Variable Declared And Initialized With A Literal
slice_declaration_literal
Slice Initialized With make And A Length
slice_declaration_make_len_only
Slice Initialized With A Length And A Larger Capacity
slice_declaration_make_len_only
Slice Initialized With Length 0 And A Larger Capacity
slice_declaration_make_cap_zero
A Nil-slice
slice_declaration_var
Append With Allocation Step 1: Allocate New Slice With Storage
slice_append_allocate
Append With Allocation Step 2: Copy Old Values And Assigne New Ones
slice_append_copy
Append With Allocation Step 3: Return The New Slice To Overwrite The Old One
slice_append_replace
Append Without Allocation
slice_append_with_capacity
addOneToAll Modifying The Same Values In Memory
slice_append_in_function1
addOneToAll Modifying A New Slice After Append
slice_append_in_function2
addOneToAll Appending And Modifying, But New Value Is Inaccessible
slice_append_in_function2
Slice Expression Syntax Illustrated
slice_expression_ranges
New Slices Created With Slice Expressions
slice_expressions_slice
New Slice Created From An Array
slice_expressions_array
New Slices Created With And Without A max Value
slice_expressions_slice
Copy Illustrated
copy_illustrated
Different Zero-length Slices Illustrated
slice_nil_vs_empty
Example Map Illustrated
map_illustrated
Relationship Between Keys And Values
keys_and_values
Simple Representation Of The Internal Structure Of A Map
map_hash_table
Simple Representation Of The Internal Structure Of A Map
map_hash_function
Map Declarations Visualized
map_declarations

Summary

  • Arrays are constant-sized collections of data that form the basis of the slice and map types.
  • Slices are powerful and flexible ordered collection types, and the primary ordered collection type in Go.
  • append works by returning a new slice header, though it does not always allocate new storage.
  • Maps are Go’s associative array or "dictionary" type, and allow you to store values of any type using a key type best suited for your use-case.
  • Slices and maps are fundamentally reference types that point to the data they contain, and it helps to be mindful of how they work and when you should modify them.

FAQ

What are the three collection types in Go covered in this chapter?Go provides arrays, slices, and maps. Arrays are fixed-size ordered collections, slices are dynamic ordered collections built on arrays, and maps store key-value pairs for fast lookup.
How is an array different from a slice in Go?An array has a fixed length that is part of its type, while a slice is a flexible view over an underlying array. Slices can grow or shrink, but arrays cannot change size once declared.
Why are arrays considered less flexible than slices?Arrays cannot expand or shrink after creation, so they are not ideal when the number of items is unknown or changes over time. Slices solve this by providing dynamic length and capacity.
What does it mean that array indexes start at 0?It means the first element in an array is accessed with index 0, the second with 1, and so on. This is standard in Go and most C-like languages.
How do you create and initialize a slice in Go?You can use a slice literal like []int{10, 20, 30} or the make function such as make([]int, 3). A slice literal creates a slice with values, while make allocates a slice with zero values.
What is the difference between slice length and capacity?Length is the number of elements currently in the slice, while capacity is how many elements the underlying array can hold from the slice’s starting point. Capacity affects whether append can grow the slice without allocating new storage.
What happens when you use append on a slice?append adds one or more elements and returns a new slice. If the existing capacity is enough, it reuses the same backing array; otherwise, Go allocates a larger array, copies the values, and returns the updated slice.
Why can modifying a slice inside a function sometimes affect the caller and sometimes not?Slices share an underlying array, so changing existing elements inside a function is usually visible to the caller. But if the function uses append and causes reallocation, the function may end up working on a different backing array unless it returns the new slice.
How do slice expressions work?Slice expressions use the form a[low:high] to create a new slice that refers to part of an existing array or slice. They share storage with the original data, and a three-index form a[low:high:max] can limit capacity.
How do maps work in Go, and what should you know about missing keys?Maps store key-value pairs and provide lookup by key rather than by position. If a key does not exist, Go returns the zero value for the element type; if you need to know whether a key exists, use the two-value assignment form: value, found := m[key].

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Go in Action, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Go in Action, Second Edition ebook for free
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Go in Action, Second Edition ebook for free