Is there an established pattern for getting pointers to non-composite literals?
x := 5
y := &x
The above code works, but is awfully verbose.
If I understand correctly that the only point of x
is to allocate the int
, I recommend something even more "verbose":
y := new(int)
*y = 5
I don't see getting it any shorter than what you have. Since the &
operator requires its operand to be either addressable or a composite literal, you're either stuck doing what you're doing to get something addressable, or you can do what I suggest and avoid the &
altogether.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With