Even I use &StructType{} most of the times. During initialization if the values of the fields are not known, new(StructType) is good to use as it initialises them to zero values of the fields.
I’ve attempted to use new(StructType) for the last 2 days and I’ve found that it does make intention easier to read when skimming through code. It especially helps when unmarshaling json/protobuf into a struct as I quite often miss out the & to create a pointer in my haste and wonder why it doesn’t work later! new eliminates this as it is always a pointer! 😋
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Even I use
&StructType{}
most of the times. During initialization if the values of the fields are not known,new(StructType)
is good to use as it initialises them to zero values of the fields.I’ve attempted to use
new(StructType)
for the last 2 days and I’ve found that it does make intention easier to read when skimming through code. It especially helps when unmarshaling json/protobuf into a struct as I quite often miss out the&
to create a pointer in my haste and wonder why it doesn’t work later!new
eliminates this as it is always a pointer! 😋