Lesson 9 of 60 · Go
Pointers, Allocation, and the Garbage Collector
Duration: 20 minutes
Pointers
This lesson explains the main concept clearly with beginner-friendly examples and background so you can learn why the code works.
Go exposes pointers (*T) but does not allow pointer arithmetic.
var x int = 10
p := &x // p points to x
fmt.Println(*p) // 10
*p = 20 // changes x to 20
new vs. make
new(T)returns a pointer to a zero‑valuedT.makeallocates and initializes slices, maps, and channels.
p := new(int) // *int, value 0
s := make([]int, 0, 10) // slice with capacity 10
Garbage Collection (GC)
Go’s GC is concurrent, non‑generational and tuned for low‑latency. You generally do not need to free memory manually; just let references go out of scope.
When to be GC‑friendly
- Reuse large buffers (
bytes.Buffer) instead of allocating per request. - Avoid retaining references to huge objects longer than needed.