Coding/Learn/go/go pointers and memory
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

LearnPointers & Value vs Pointer Semantics
8 / 20 0%
Lesson 08 of 20
14 mins read

Pointers & Value vs Pointer Semantics

PreviousNext Lesson
In Plain English

Go features safe pointers without pointer arithmetic. Master address-of (&), dereferencing (*), heap vs stack escape analysis, and how to choose between value copies and pointer mutation.

The Mental Model

"Passing by value is like photocopying a document and handing over the copy. Passing a pointer is giving someone the GPS coordinates to the original file cabinet."

Deep Dive: How It Works

Address-of & Dereference: &x gets the pointer address; *p accesses the underlying value.

No Pointer Arithmetic: Go pointers cannot be arbitrarily offset like C pointers, ensuring memory safety.

Escape Analysis: The Go compiler automatically moves local variables to the heap if their pointers escape the function scope.

Syntax Blueprint

var x int = 42
p := &x
*p = 100 // x is now 100

Use & to capture the address of a variable; use * to dereference and mutate the underlying value.

PointCore Rules to Remember

Point star
Safe Pointers: Memory-safe pointers with zero manual free() needed; garbage collector cleans up heap memory.
Point star
Automatic Heap Escape: Returning &localStruct from a function is completely safe in Go.
Point star
Value vs Pointer: Use values for immutability/small structs; use pointers for state mutation or large payloads.

Common Beginner Traps & How to Fix Them

Mistake:Dereferencing a nil pointer (*nilPointer).

Why it happens: An uninitialized pointer holds nil (address 0x0).

How to fix: Always verify if ptr != nil before dereferencing if the pointer can be optional.

Pro Tip: Default to value semantics unless you need to mutate caller state or the struct contains a sync.Mutex.

Live Interactive Example

Hit Run Code to see it live
Comparing Value vs Pointer Parameter Mutations
Go 1.22 / Runtime
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Output Console
Click "Run Code" to view the rendered output.
How it works: incrementPointer received &score and dereferenced it with *v to mutate score directly.

Your Turn: Micro Challenge

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

Micro Exercise

Implement In-Place Pointer Swap

  • PointWrite a function swap(a, b *int) that swaps the values of the two integer pointers in place.
  • PointIn main(), initialize x := 10 and y := 20.
  • PointCall swap(&x, &y) and print "x: %d, y: %d".
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