Chapter 4 – Collections, Pointers & Memory

Welcome to Week 4 of the 8-Week Go Mentorship Program.

This chapter is one of the most important in your Go journey.

If you truly understand the concepts here, you will avoid the majority of beginner-level Go mistakes.


🎯 Chapter Goal

By the end of this chapter, you should:

  • Understand the difference between arrays and slices
  • Know how slice memory sharing works
  • Avoid common append() mistakes
  • Understand how maps behave in memory
  • Use pointer receivers correctly
  • Avoid common nil panics
  • Understand value vs reference semantics in Go
  • Gain intuition about Go’s memory model

This chapter builds the mental model required before learning concurrency.


📚 What You Will Learn

1️⃣ Arrays vs Slices

  • Arrays as value types
  • Why arrays are rarely used in production
  • Slice internals (pointer, length, capacity)
  • Slice backing array behavior

2️⃣ Slice Pitfalls

  • Sharing underlying arrays
  • Capacity growth
  • When append() reallocates
  • Safe deep copy patterns

3️⃣ Maps

  • Initialization rules
  • Why writing to nil maps panics
  • Safe lookups
  • Why maps are reference types
  • Why maps are not thread-safe

4️⃣ Pointers

  • Basic pointer syntax
  • When to use pointer receivers
  • Value vs pointer receiver differences
  • Large struct performance considerations

5️⃣ Nil Pitfalls

  • Nil slice vs empty slice
  • Nil map behavior
  • Nil pointer panic
  • The interface nil trap (advanced)

6️⃣ Memory Concepts

  • Stack vs heap (conceptual)
  • Escape analysis
  • Why returning pointer to local variable is safe

📂 Folder Structure

chapter-4-collections-pointers/
│
├── README.md
├── study-material.md
├── exercises.md
├── solutions.md
└── solutions_test.go

🧠 How to Use This Chapter

Step 1 – Study

Read:

study-material.md

Take notes. Do not rush.


Step 2 – Practice

Complete:

exercises.md

Try solving without looking at solutions.

If stuck:

  • Re-read concepts
  • Use fmt.Println() debugging
  • Run small experiments

Step 3 – Review

Compare your answers with:

solutions.md

Focus on:

  • Why your solution differs
  • Memory implications
  • Receiver choice decisions

Step 4 – Run Tests

Run:

go test ./...

Or with race detector:

go test -race ./...

Tests are designed to reinforce:

  • Value semantics
  • Slice sharing behavior
  • Pointer safety
  • Map initialization rules
  • Interface nil behavior

⚠️ Common Mistakes This Chapter Fixes

If you skip this chapter deeply, you will likely:

  • Write to nil maps
  • Misuse append()
  • Accidentally share slice memory
  • Use wrong method receivers
  • Panic due to nil pointer
  • Misunderstand interface nil behavior

This chapter prevents those problems.


🔍 Key Mental Model

In Go:

  • Arrays and structs → copied
  • Slices → descriptor copied, data shared
  • Maps → reference type
  • Pointers → reference
  • Nil behaves differently across types

Understanding this is more important than memorizing syntax.


🚀 Why This Matters Before Week 5

Concurrency magnifies memory mistakes.

If you do not understand:

  • Slice backing arrays
  • Map thread safety
  • Pointer semantics

Then concurrency will feel confusing and unsafe.

This chapter makes Go predictable.


📈 Expected Outcome

After completing this chapter, you should be able to:

  • Debug slice behavior confidently
  • Avoid nil-related panics
  • Choose correct receiver types
  • Write safe map access patterns
  • Understand why Go behaves the way it does

👨‍💻 Maintained By

Aditya Pratap Bhuyan Mentorship Initiative – Practice GoLang for Beginners Licensed under GNU GPL v3



Table of contents


This site uses Just the Docs, a documentation theme for Jekyll.