Tutorial by Examples

for is the only loop statement in go, so a basic loop implementation could look like this: // like if, for doesn't use parens either. // variables declared in for and if are local to their scope. for x := 0; x < 3; x++ { // ++ is a statement. fmt.Println("iteration", x) } //...
Breaking out of the loop and continuing to the next iteration is also supported in Go, like in many other languages: for x := 0; x < 10; x++ { // loop through 0 to 9 if x < 3 { // skips all the numbers before 3 continue } if x > 5 { // breaks out of the loop once x...
The for keyword is also used for conditional loops, traditionally while loops in other programming languages. package main import ( "fmt" ) func main() { i := 0 for i < 3 { // Will repeat if condition is true i++ fmt.Println(i) } } play ...
Simple form using one variable: for i := 0; i < 10; i++ { fmt.Print(i, " ") } Using two variables (or more): for i, j := 0, 0; i < 5 && j < 10; i, j = i+1, j+2 { fmt.Println(i, j) } Without using initialization statement: i := 0 for ; i < 10; i++ {...
package main import( "fmt" "time" ) func main() { for _ = range time.Tick(time.Second * 3) { fmt.Println("Ticking every 3 seconds") } }

Page 1 of 1