Choosing a value or pointer receiver in Go
In Go, arguments are always passed by value. This means that values are always copied.
If you pass a pointer, the value of the pointer will be passed by copy, but the method will be able to access the value the pointers points at without copying.
There are two reasons to use a pointer receiver:
- The method can modify the value that its receiver points to
- Avoid copying the value on each method call. This can be more efficient if the receiver is a large struct.
In general, all methods on a given type should have either value or pointer receiver, but not a mixture of both
References
Next -> interfaces-in-go
#golang #stack #programming #reference #pass_by #heap #for_the_love_of_go #value #pointer #methods