David in Canada

 

Tech, life, and everything in between.

Day 9 — Understanding Pointers and Struct Mutation in Go

Updated at # Tech # Go

Overview

Today I learned one of the most important concepts in Go: pointers.

Pointers are essential for understanding how data is passed and modified in Go. This lesson focused on how Go handles values vs references, and how to properly mutate struct data.


What I Built

An enhanced Student Manager CLI that:


Key Concepts

Pointer Basics

x := 10
p := &x

Pass by Value vs Pointer

func changeScoreValue(s Student)
func changeScorePointer(s *Student)

Pointer Receiver Methods

func (s *Student) addScore(points int)

Allows direct modification of struct fields.


Slice of Pointers

[]*Student

Used for:


Converting Slice to Pointer Slice

for i := range students {
    ptrs = append(ptrs, &students[i])
}

Important to avoid:

for _, s := range students {
    &s // incorrect
}

Key Demonstration

Before: 80
After value function: 80
After pointer function: 90

This clearly shows how pointers allow mutation.


Key Takeaways


Reflection

Today’s lesson clarified how Go handles memory and data mutation.

Understanding pointers is critical for writing correct and efficient programs. This concept connects directly to real-world backend development.


Next Steps