This is a creation in Article, where the information may have evolved or changed. Go by Example:for
For was Go ' s only looping construct. Here is three basic types of for loops.
The most basic type, with a single condition.
A Classic Initial/condition/after for loop.
For without a condition would loop repeatedly until you break out of the the loop or return from the enclosing function.
Package Mainimport "FMT" Func Main () { I: = 1 for i <= 3 { FMT. Println (i) i = i + 1 } for J: = 7; J <= 9; j + + { FMT. Println (j) } for { FMT. Println ("loop") Break }}
Translated:
The FOR keyword is the only loop structure in the go language, the following is the three basic type structure for a for loop
The most basic is a single condition
One is the initial value/condition/Then the self-processing condition for the For loop
The For loop will loop until the break is out of the loop for no condition, or the perimeter function returns
Package Mainimport "FMT" Func main () {i: = 1for I <= 3 {//= equivalent to While loop FMT. PRINTLN (i) i = i + 1}for j: = 7; J <= 10; J + + {//traditional for loop fmt. Println (j)}for {//similar to do ... While Loop FMT. Println ("Loop") Break}}
Run results
Go Run for.go
1
2
3
7
8
9
10
Loop
Original address