Skip to main content
Go Language Fundamentals for Beginners to Advanced
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 Length and Capacity.
  • 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
123456789101112131415
package main
import "fmt"

func main() {
    // Declares an array that holds exactly 3 strings
    var cars [3]string 

    cars[0] = "Volvo" // Arrays are zero-indexed!
    cars[1] = "BMW"
    cars[2] = "Ford"
    // cars[3] = "Mazda" // ERROR: index out of bounds!

    fmt.Println(cars)
    fmt.Println("Total cars:", len(cars)) // len() gets the length
}

Short Initialization:

go
1
primes := [5]int{2, 3, 5, 7, 11}

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
123456789
func main() {
    // Empty brackets mean this is a SLICE, not an array
    users := []string{"Alice", "Bob"} 

    // We can ADD to a slice using the append() function!
    users = append(users, "Charlie") 
    
    fmt.Println(users) // Output: [Alice Bob Charlie]
}

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
12345678
func main() {
    nums := []int{1, 2}
    fmt.Printf("Len: %d, Cap: %d\n", len(nums), cap(nums))
    
    // Add an item. The capacity will double automatically!
    nums = append(nums, 3)
    fmt.Printf("Len: %d, Cap: %d\n", len(nums), cap(nums))
}

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
123
// Creates a slice of ints. Length is 0, but starting Capacity is 10,000.
// This prevents memory resizing during appending!
data := make([]int, 0, 10000)

8. Iterating over Arrays and Slices

We use the for ... range loop to iterate.
go
12345
heroes := []string{"Batman", "Superman", "Flash"}

for index, name := range heroes {
    fmt.Printf("Hero #%d is %s\n", index, name)
}

*(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]int is an array (rigid). []int is 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, use make() to set the capacity and save the Garbage Collector from doing extra work.

11. Exercises

  1. 1. Create a Slice of strings containing 3 grocery items.
  1. 2. Use append() to add 2 more items.
  1. 3. Use a for ... range loop 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. 1. Explain the difference between an Array and a Slice in Go.
  1. 2. How does the append() function affect memory allocation when a slice reaches its maximum capacity?
  1. 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. Using append(), slices grow automatically, while functions like len(), cap(), and make() allow developers to monitor and optimize memory usage perfectly.

15. Next Chapter Recommendation

Slices are great for ordered lists, but looking up an item requires scanning the whole list. In Chapter 11: Maps in Go, we will learn how to store data in Key-Value pairs for lightning-fast, instantaneous lookups.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·