4 ms·
I like the second option (&int(3)) the most personally, as I find myself occasionally defining a bunch of variables before I can use them as pointers in structs
by candiddevmike 6y ago
I like the second option (&int(3)) the most personally, as I find myself occasionally defining a bunch of variables before I can use them as pointers in structs. It looks and feels a lot cleaner to use this vs having new everywhere.
- cassianoleal 6y agoThat would be my preference too. Great readability, and most users would eventually try this out even before searching for the right way (I have tried it). I tend to not declare variables when the pointer is used deep into a struct because I find the back-and-forth in the editor to be bad. I usually resort to a pointer to an inline anonymous function, e.g.: a := SomeStruct{ Field: func() *int64 { x := int64(13); return &x }(), } It's ugly and verbose but after seeing it 2 or 3 times you immediately know what it's about the next time.