var i = 0; <--- valid
i := 0 <--- Valid
for (i := 0; ...) <--- Valid
for (var i = 0; ...) <--- Invalid
No? var i = 0; <--- valid
i := 0 <--- Valid
for (i := 0; ...) <--- Valid
for (var i = 0; ...) <--- Invalid
No?If I rewrite your comment in correct Go I get
var i int = 0 // valid
i := 0 // valid
for i := 0; ... { // valid
for var i int = 0; ... { // Not valid
Your final line there is only needed if you want to define the type of i at declaration time inside a for loop. That would be better written for i := int(0); ... {Either allow the use of var to declare a variable, or dont.
Dont mix and match in different contexts; this clearly violates the go goal of consistent syntax.
Edit, incidentally the use of var is perfectly legal like this:
package main
import "fmt"
func main() {
var a = 10
b := 10
fmt.Printf("%d %d\n", a, b);
for i := 0; i < 10; i++ {
fmt.Printf("%d Value\n", i)
}
} 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.