Although A Tour of Go mentions when to use a pointer receiver over a value receiver and many people share their opinions, I’ve been confused about it since I started learning Go. It may includes some mistakes, but I’d like to write my current opinion.
- Design with referential transparency to reduce side-effects from our code; especially for modules called by others.
- Use a value receiver as default, and use a pointer receiver instead only when it’s necessary (to be honest, I cannot judge it yet).
References
Matt Boyle (@MattJamesBoyle) on X
Pointer vs Value Receivers First long-form #golang tweet of 2024 :) When mentoring folks, we usually pause for a little bit to talk about the difference between these two. Here's an example pointer receiver: When you use a pointer receiver in a method, you're working with the
x.com