variable declaration in init statement of for loop

后端 未结 2 1221
我在风中等你
我在风中等你 2020-12-20 23:28

Yet another beginner\'s question for golang:

I can write:

for i := 0; i < 10; i++ {}

But if I want i to be of a spe

2条回答
  •  长情又很酷
    2020-12-21 00:10

    The language specification for a for loop states that: The init statement may be a short variable declaration, which is assignment of the form i := 0 but not a declaration of the form var i = 0. As for the reason behind this - I'm guessing language simplicity. See here: http://golang.org/ref/spec#For_statements

    BTW You can do something like this:

    for  i := int64(0); i < 10; i++ {
       // i here is of type int64
    }
    

提交回复
热议问题