25% off

Use code FUNCMAIN at checkout for 25% off all premium courses.

Get started →
Pointer Receivers vs Value Receivers in Go
Code Snippet

Pointer Receivers vs Value Receivers in Go

go

Go allows you to define methods on both pointer and value receivers. A value receiver operates on a copy of the struct, so any modifications are lost after the method returns. A pointer receiver operates on the original struct, allowing you to modify its fields and have those changes persist.

Use value receivers when your method doesn’t need to modify the struct and the struct is small enough that copying is efficient. Use pointer receivers when your method needs to modify the struct, when the struct is large, or when consistency demands that all methods on a type use the same receiver type. Following these guidelines ensures your code is predictable and efficient.

Further Reading: