For statements with
for
clause… Any element of the
ForClause
may be empty but the semicolons are required unless there is only a condition. If the condition is absent, it is equivalent to the boolean value true.for cond { S() } is the same as for ; cond ; { S() } for { S() } is the same as for true { S() }
So this goes to show that the single condition form of the for loop is really just a special case of the “for clause” for loop, with the init and post statements omitted.
But let’s look at some other examples of omitting elements of the for clause. These are all valid:
for x := 0; ; { // Initialize variable x, then start an infinite* loop
for ; x < 3; { // Same as `for x < 3 {`
for ; ; x++ { // Increments x after every iteration of the looop
for x := 0; ; x++ { // Initializes variable x, then increments it after every iteration of the infinite* loop
for ; ; { // Same as `for true {` or `for {`
*such loops can still be terminated by a terminating statement such as break
or return
.
Quotes from The Go Programming Language Specification Language version go1.22 (Feb 6, 2024)