CHAPTER 10
Beginner
Arrays and Slices
Updated: May 17, 2026
5 min read
# CHAPTER 10
Arrays and Slices
1. Introduction
If you are building a school portal, you don't want to create 100 separate variables (student1, student2, student3) for a classroom. You need a way to group related data under a single name. Go provides two structures for this: Arrays and Slices. Understanding the difference between them is one of the most crucial concepts in Go.
2. Learning Objectives
By the end of this chapter, you will be able to:- Declare and initialize fixed-size Arrays.
- Understand why Arrays are rigid.
- Declare and manipulate dynamic Slices.
-
Use the
append()function.
-
Understand the difference between
LengthandCapacity.
-
Use the
make()function for optimization.
3. Arrays (Fixed Size)
An array is a numbered sequence of elements of the *same data type*. Crucial Rule: The size of an array is FIXED when it is created. You cannot add more elements to it later.
go
Short Initialization:
go
4. Slices (Dynamic Size)
Because Arrays cannot grow or shrink, they are rarely used directly in modern Go code. Instead, we use Slices 99% of the time. A Slice is a dynamic, flexible view into the elements of an array. A Slice can grow!Syntax Difference: You leave the brackets [] EMPTY!
go
5. How append() works in Memory
A Slice doesn't actually store data itself. It acts as a pointer to a hidden underlying Array.
When you use append(), Go checks if the hidden array has enough room. If it is full, Go creates a brand new, larger array in memory, copies the old data over, adds your new item, and deletes the old array. This happens in milliseconds behind the scenes!
6. Length vs Capacity
Because of this hidden array behavior, Slices have two properties:-
Length (
len): How many items are currently accessible in the slice.
-
Capacity (
cap): How much total room exists in the hidden underlying array before Go has to create a new one.
go
7. The make() Function (Optimization)
If you know you are going to add 10,000 items to a slice, letting Go automatically resize the hidden array 15 times is bad for performance. You can pre-allocate memory using the make() function.
go
8. Iterating over Arrays and Slices
We use thefor ... range loop to iterate.
go
*(Remember: If you don't need the index, replace it with the blank identifier _)*.
9. Common Mistakes
-
Index Out of Range: Trying to access
cars[5]when the length is only 3 will instantly crash (Panic) the program.
-
Confusing Arrays and Slices:
[3]intis an array (rigid).[]intis a slice (dynamic). They are completely different types in Go, and you cannot pass an array to a function that expects a slice.
10. Best Practices
- Always use Slices: Unless you are building highly optimized cryptographic algorithms or parsing fixed-byte network packets, always default to using Slices.
-
Use
make()for known sizes: If you know exactly how much data you will pull from a database, usemake()to set the capacity and save the Garbage Collector from doing extra work.
11. Exercises
- 1. Create a Slice of strings containing 3 grocery items.
-
2.
Use
append()to add 2 more items.
-
3.
Use a
for ... rangeloop to print them all out.
12. MCQs with Answers & Explanations
Question 1
What is the primary limitation of an Array in Go?
Question 2
At what index does a Go array/slice begin?
Question 3
Which of the following defines an Array?
Question 4
Which of the following defines a Slice?
Question 5
What function is used to add new items to a Slice?
Question 6
What does the len() function return?
Question 7
What does the cap() function return?
Question 8
What happens under the hood when a Slice's capacity is full and you call append()?
Question 9
Which function is used to pre-allocate memory for a slice to optimize performance?
Question 10
What loop structure is best for reading all items in a Slice?
13. Interview Preparation
Interview Questions:- 1. Explain the difference between an Array and a Slice in Go.
-
2.
How does the
append()function affect memory allocation when a slice reaches its maximum capacity?
-
3.
What is the purpose of the
make()function, and when should you use it?
14. Summary
Arrays provide fixed-size memory storage, but they are rigid. Slices are flexible, dynamic windows built on top of Arrays. Usingappend(), slices grow automatically, while functions like len(), cap(), and make() allow developers to monitor and optimize memory usage perfectly.