Golang / Golang Internals and Memory Management Interview Questions
How do pointers work in Go and when should you pass by pointer vs by value?
Go is pass-by-value: every function argument is a copy. For types that are large or need to be mutated by the called function, passing a pointer avoids the copy and allows in-place modification. Understanding when to use pointers is essential for both correctness and performance.
// Pass by VALUE — mutation inside the function is invisible to caller
func doubleValue(n int) { n *= 2 } // n is a copy
x := 5
doubleValue(x)
fmt.Println(x) // still 5
// Pass by POINTER — mutation is visible
func doublePtr(n *int) { *n *= 2 }
doublePtr(&x)
fmt.Println(x) // 10
// Large structs — avoid copying by passing pointer
type BigStruct struct { data [1024]byte }
func processValue(b BigStruct) {} // copies 1 KB every call
func processPtr(b *BigStruct) {} // copies only 8 bytes (pointer)
// Value receiver vs pointer receiver on methods
type Counter struct{ count int }
func (c Counter) Get() int { return c.count } // value receiver — copy
func (c *Counter) Inc() { c.count++ } // pointer receiver — mutates
c := Counter{}
c.Inc() // Go automatically takes &c
fmt.Println(c.Get()) // 1
// Pointer vs nil — always check before dereferencing
var p *int
fmt.Println(p) //
// fmt.Println(*p) // PANIC: nil pointer dereference | Use pointer when... | Use value when... |
|---|---|
| The function must mutate the receiver/argument | The function is read-only |
| The type is large (>64 bytes typical threshold) | The type is small (int, bool, small struct) |
| The type has a sync.Mutex or similar non-copyable field | Immutability is desirable |
| Consistency: other methods use pointer receiver | Type is inherently value-like (time.Time, net/netip.Addr) |
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
