Sunday, February 8, 2026
Pointer
Akash AmanUpdated: February 2026
Pointers
The Pointer Type (*T)
Concept: A variable that doesn't store data itself (like a number or string), but stores the memory address of another variable.
- Behavior:
- The Address Operator (
&): Used to "find" where a variable lives in memory. - The Dereference Operator (
*): Used to "look inside" the address and see or change the value stored there.
- The Address Operator (
- Why: To avoid making expensive copies of data. Instead of handing someone a massive 500-page book (the data), you give them a sticky note with the library shelf location (the pointer).
The new() Function
Concept: A built-in function that allocates memory for a type and returns a pointer to it.
- Behavior:
new(T)allocates "zeroed" storage for a new item of typeTand returns its address (*T).
- Why: Useful when you want to create a pointer to a type without having to declare a named "dummy" variable first.
Pass-by-Value vs. Pass-by-Pointer
Concept: By default, Go is "pass-by-value." When you pass a variable to a function, Go makes a complete copy of it.
- Behavior:
- Why:
- Pass-by-Value: Safer. The function can't accidentally mess up your original data.
- Pass-by-Pointer: Necessary when a function needs to modify the original variable, or when the data is so large that copying it would slow down the program.
The "Nil" Pointer
Concept: A pointer that has been declared but doesn't point to any specific memory address yet.
- Behavior: The zero-value of any pointer is
nil. Trying to dereference (*p) anilpointer will cause the program to crash (Panic). - Why: It acts as a "null" or "empty" state. It tells the program: "I have a spot for an address, but I'm not looking at anything right now." This is often used to signify that something was not found or hasn't been initialized.
Pointer Receivers in Structs
Method Receivers ((t *T) Method())
Concept: Deciding whether a method belongs to the value or the pointer of a struct.
-
Behavior:
- Value Receiver: Even if you change
HealthinsideTakeDamage, the originalPlayerremains at full health. - Pointer Receiver: The
Healmethod actually updates the originalPlayerstruct in memory.
- Value Receiver: Even if you change
-
Why: Consistency. If any single method on a struct needs a pointer receiver to modify data, it is a Go convention to use pointer receivers for all methods on that type to avoid confusion.
Reference Guides
Pointer Symbols Cheat Sheet
| Symbol | Name | Action | Literal Translation |
|---|---|---|---|
*int | Type | Declares a pointer | "This variable holds the address of an int." |
&myVar | Address Of | Gets the memory address | "Tell me where myVar is sitting in memory." |
*myPtr | Dereference | Accesses the value | "Go to the address inside myPtr and show me what's there." |
new(int) | New | Allocates & returns pointer | "Give me a fresh address for an int initialized to 0." |
When to Use Pointers?
| Use a Pointer if... | Use a Value if... |
|---|---|
| You need to modify the original variable. | The variable is a small, basic type (int, bool, string). |
| The struct is large (saves memory/CPU). | The data should be immutable (should not change). |
You need to represent absence (nil). | You want to avoid the risk of nil-pointer panics. |