Menu

1 / 2

Understanding Pointers in Go; The Two Runes (& and *) of Go

DEV Community·Emmanuel Ayinde·6 months ago
#WKrBi1Zz
#pointer#how#pointers#address#value
Reading 0:00
15s threshold

A thorough guide to addresses, pointers, and the elegant dance between them - grounded in how your computer's RAM actually works, with real examples, and an honest look at the trade-offs.

Table of Contents

Let's Start with a Story

How RAM Actually Works

RAM is a flat, indexed array of bytes

How the CPU reads and writes memory

What a "variable" actually is at runtime

Memory alignment

The Stack and The Heap

The Stack

The Heap

Escape analysis: Go decides for you

From RAM to Pointers - The Bridge

The & Operator

What can & be applied to?

The composite literal shortcut

The * Operator

The full picture together

Pointer Types

nil Pointers

Pointers in Functions

The problem: pass by value

The solution: pass the address

Performance: avoiding large copies

Pointers & Structs

Linked list - the canonical pointer use case

Pointer Receivers

The new() Function

Advantages of Pointers in Go

Mutation across function boundaries

Avoiding expensive copies

Expressing optional values

Shared mutable…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.