PointersIntermediate
1Question 1 of 1
goHow do Pointers work in Go and how does escape analysis determine heap vs stack allocation?
Explanation & Concept
Go has pointers allowing you to pass memory references (`&val`) instead of copying data. Unlike C, Go does not support pointer arithmetic.
The Go compiler performs **Escape Analysis**: if a variable cannot be referenced outside its defining function frame, it is allocated on the fast stack. If a reference escapes the function (e.g. returning a pointer), the compiler transparently allocates it on the heap.
Code Example
go
package main
type Config struct {
MaxRetries int
}
// Compiler escape analysis puts 'cfg' on the heap because the pointer escapes
func NewConfig() *Config {
cfg := Config{MaxRetries: 3}
return &cfg // Completely safe in Go!
}How It Works & Key Takeaways
Returning pointers to local variables is safe in Go because the compiler detects the escape and automatically promotes the allocation to the heap.
Mock Test · 1 Questions · Optional
Ready for Maps?
Take a quick test to evaluate yourself, or move straight to the next module.
Intermediate1 questions