In short: None.
First: Go has no "links." Forget about this idea now, otherwise you will hurt yourself. Indeed. Thinking about "by reference" is simply wrong.
Second: performance is exactly the same. Forget about this kind of nano optimization now. Especially when working with int. If and only if you have a performance problem: Measure and then optimize. Perhaps it would be intuitively appealing to think: "Manual operation with a tiny pointer of 8 bytes should be much faster than copying structures with 30 or even 100 bytes." This is not so, at least it is not so simple.
Third: just write it func f() *int { d := 6; return &d; } func f() *int { d := 6; return &d; } func f() *int { d := 6; return &d; } . There is no need to do any fantastic dances.
Fourth: Option 2 makes a deep copy of int. But this can be misleading since there are no โsmall copiesโ for int, so I'm not sure if I understand what you are asking here. Go has no concept of deep and shallow copy. If you copy the pointer value, the pointer value will be copied. Do you remember the first point? There are no links in Go. The pointer value is the value, if it is copied, you have a copy of the pointer value. Such a copy does nothing for the value it points to, especially it does not make a copy. This suggests that the copies in Go are not deep. Forget about deep / shallow copy when talking about Go. (Of course, you can implement functions that perform a โdeep copyโ of your custom objects)
Fifth: Go has a properly working garbage collector. It does not matter what you do: as long as the object is alive, it will not be assembled, and once it is assembled, it will be. You can send, return, copy, transmit, receive address, dereference pointers, or whatever you like, it just doesn't matter. GC is working correctly. (If you are not intentionally looking for pain and errors, use an unsafe package.)
Volker
source share