4 ms·
The Go language brings back the ":=" to mean declare and assign, while inferring type. a := 5 You can also equivalently do var a = 5 I almost never use the
by danans 9y ago
The Go language brings back the ":=" to mean declare and assign, while inferring type.
a := 5
You can also equivalently do
var a = 5
I almost never use the latter within functions (only at module scope).
I believe the former is forbidden except within functions.
- munk-a 9y agoI'm not a big fan of this decision by golang, with the frequency of multi-assignment happening in common code make this awkward. Specifically: var a = 5 a, b := 2, 3
- jug 9y agoI was puzzled by this decision of theirs though. They are so hell bent on keeping the language as simple as possible with a minimum of keywords, but apparently can't be arsed to just write var a = 5 like normal people, but prefer to invent a whole new operator for it. Operators are often demanding even more of a reason to exist than keywords. Then you also get the operator inconcistency between "assigning to a new variable" and "assigning to an existing variable" and is it so important to tell the two apart from within an operator, of all things? When writing Go, I have been way more inconvenienced by its error handling mechanics than having to tell that I want a new variable using a few more characters.