对于循环的前和后语句为空

In Golang what does it means that the pre and post statements of a for loop are empty like in this example:

    sum := 1
    for ; sum < 10; {
        sum += sum
    }
    fmt.Println(sum)

Remember that a for loop is the same as a while loop. Your code can be rewritten in other languages as

sum := 1
while(sum < 10) {
    sum += sum
}
fmt.Println(sum)

In a for loop, there are 3 parts.

for(initial statement ; condition ; end statement usually iterate)

This is equivalent to

initial statement
while(condition) {
    Stuff here
    End iteration statement
}

The reason your loop can be written withiut the pre and post statements is because you've specified them in other parts of the code.

It behaves like a while in other languages, you don't need the two semicolons:

sum := 1
for sum < 10 {
    sum += sum
}
fmt.Println(sum)

For loop has 3 elements: initialization statement, condition check, variable change.

for <initialization statement>; <condition check>; <variable change>{
    <actual body>
}
  • initialization statement is executed only once when the loop starts. Based on the name it initialize something (in a lot of cases a variable you iterate through). If it is omitted, then it does nothing
  • condition check verifies whether the condition evaluates to true. If it is not, the loop stops. If it is omitted, then it is always true.
  • variable change is modifying variables during each iteration of the loop. Most of the time, the iterated variable is increased/decreased, but you can do whatever you want. If it is omitted, does nothing

After this explanation, you can see that this loop does nothing during your initialization, and post condition phase.

You also do not need to use semicolons here. This will be enough.

sum := 1
for sum < 10 {
    sum += sum
}

You can even write a loop like this: for {} which will never stop executing, or do something like a while loop:

t := 10
for t > 0{
  t--
}

Note that inside your initialization, condition, and change phase you can use many expressions (not just one). So with a simple while loop you can do something like:

for f1, f2, n := 1, 1, 10; n > 0; f1, f2, n = f2, f1 + f2, n - 1{
    fmt.Println(f1)
}

which creates a fibonacci numbers go playground. Showing this not because this is the best way to write it, but rather because it is possible.