top | item 39114787

(no title)

devjam | 2 years ago

The author used:

    sum := float32(0)
Over Go's zero-value default initialization e.g.

    var sum float32
A nit stylistically but wondering if there was a good reason to do so?

discuss

order

meheleventyone|2 years ago

One advantage is that it makes the value explicit so anyone reading the code irregardless of their familiarity with Go will know it will be zero.

abigail95|2 years ago

i do the same because i find the var keyword ugly

devjam|2 years ago

Sure, I understand that from the POV of making your code explicit. Personally I have always tended towards, for example:

    var x string
when initializing empty (zero-value) vars, versus:

    x := "hello"
when initializing variables that should hold an initial value.

To me as a Go programmer at least, this is more obvious and intuitive as to the intent of the declaration.