Pointer to a non-compositional literal

Is there an established pattern for getting pointers to non-composite literals?

x := 5 y := &x 

The code above works, but terribly detailed.

+7
go
source share
1 answer

If I understand correctly that the only point x is to highlight int , I recommend something even more β€œverbose”:

 y := new(int) *y = 5 

I don’t see it being shorter than yours. Since the & operator requires that its operand be either an addressable or a compound literal, you either get stuck doing what you are doing to get something addressed, or you can do what I suggest and avoid & in general.

+8
source share

All Articles