6 ms·
Actually, the following is valid syntax in Go 1.0 and later: var i = 0 The use of "var" without a type is a common idiom when binding global variables, su
by swdunlop 14y ago
Actually, the following is valid syntax in Go 1.0 and later:
var i = 0
The use of "var" without a type is a common idiom when binding global variables, such as:
var ErrTooLarge = errors.New("bytes.Buffer: too large")
Oddly, the := shortcut is not legal in the global context. Also fun:
var (
i = 0
j = 0
)
So, going back to the original comment -- yes, Go has some odd little shortcuts and inconsistencies.
- StavrosK 14y agoWhy is := not valid in the global context? I have been bitten by this time and time again. Is there a reason for it? It makes no sense to me.
- burntsushi 14y agoI believe the answer is related to the simplicity of the parser.
- StavrosK 14y agoI see, thank you. Too bad this wart is exposed, though.