Coding/Learn/go/go arrays and slices
Go Foundational Course
01.Introduction, Toolchain & Package main
02.Variables, Zero Values, Constants & iota
03.Control Flow: If with Init Statements & Switch
04.The Unified For Loop & Range Iteration
05.Functions, Multiple Return Values & Named Returns
06.Arrays vs Slices: Dynamic Memory & Headers
07.Maps: Key-Value Hash Tables & Safe Access
08.Pointers & Value vs Pointer Semantics
09.Structs, Receiver Methods & Composition
10.Implicit Interfaces & Duck Typing with Typesafety
11.Explicit Error Handling, Wrapping & Inspection
12.Defer, Panic & Recover: Resource Cleanup
13.Strings, Runes & UTF-8 Encoding Internals
14.Goroutines & The M:N Work-Stealing Scheduler
15.Channels: CSP Concurrency & Pipelines
16.Select Statement, Non-Blocking IO & Timeouts
17.Mutexes, RWMutex & Atomic Primitives
18.Context Propagation, Cancellation & Deadlines
19.JSON Serialization, Tags & net/http Routing
20.Capstone: High-Performance Concurrent Microservice Engine
Course Certificate0%

Complete all lessons to earn

LearnArrays vs Slices: Dynamic Memory & Headers
6 / 20 0%
Lesson 06 of 20
14 mins read

Arrays vs Slices: Dynamic Memory & Headers

PreviousNext Lesson
In Plain English

In Go, fixed arrays are values, while slices are lightweight windows (slice headers containing pointer, len, cap) pointing to backing arrays. Learn append semantics, capacity doubling, and reallocations.

The Mental Model

"Think of an array as a concrete driveway of fixed length. A slice is a portable frame you place over any portion of that driveway. Moving or resizing the frame is instant because the concrete stays in place."

Deep Dive: How It Works

Array vs Slice: An array [5]int has fixed size; a slice []int is a 3-word header (data pointer, length, capacity).

Append Mechanics: When append exceeds capacity, Go allocates a larger backing array, copies data, and updates the header.

Sub-slicing: s[low:high] creates a new slice header referencing the same underlying backing array.

Syntax Blueprint

s := make([]int, 0, 4)
s = append(s, 10, 20)
sub := s[0:1]

make allocates a slice with length 0 and capacity 4; append adds elements; s[low:high] sub-slices.

PointCore Rules to Remember

Point star
Lightweight Header: Slices are 24-byte descriptors (pointer, length, capacity) on 64-bit platforms.
Point star
Append Reallocation: Always capture append return value: s = append(s, val) in case a new backing array was allocated.
Point star
Shared Storage: Sub-slices share backing array storage until an append triggers reallocation.

Common Beginner Traps & How to Fix Them

Mistake:Assuming modifying a sub-slice will not affect the original slice.

Why it happens: Both slice headers point to the same underlying backing array until reallocation occurs.

How to fix: Use copy(dest, src) to clone elements into an independent slice if isolation is required.

Pro Tip: Pre-allocate slice capacity with make([]T, 0, expectedCap) to eliminate dynamic allocation overhead during bulk appends.

Live Interactive Example

Hit Run Code to see it live
Slice Header Growth & Sub-slicing
Go 1.22 / Runtime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Output Console
Click "Run Code" to view the rendered output.
How it works: When appending 30, initial cap=2 was exceeded, doubling capacity to 4.

Your Turn: Micro Challenge

No pressure! Edit the starter code below and test your solution with instant feedback.

Micro Exercise

Filter Even Numbers into a Dynamic Slice

  • PointDefine a function filterEvens(nums []int) []int that takes a slice of integers.
  • PointIterate through the slice and append only even numbers to a new result slice.
  • PointIn main(), call filterEvens with [1, 2, 3, 4, 5, 6, 7, 8] and print the filtered slice.
1
2
3
4
5
6
7
8
9
10
11
Sandbox Output
Click "Run & Check" to test your solution.

Finished reading and practicing?

Mark this lesson as completed to update your course progress.

PreviousNext Lesson
Overview100 SheetLearnCertificates