This is a creation in Article, where the information may have evolved or changed.
The loop of the Go language
The loop in Go is relatively simple only for
1. Normal for loop
for initialization statements; conditional statements; Modifier statement {}
For I: = 0; I < 5; i++ {
Fmt. Printf ("This is the%d iteration\ n", i)
}
2. Similar while loop
For i >= 0 {
i = I-1
Fmt. Printf ("The variable I am now:%d\ n", i)
}
3. Infinite loops
for{
...
}
4.for-range structure
Go is unique, and it can iterate over any set (including arrays and maps) and feel like a foreach loop.
General form for IX, val: = Range Coll {}
Used to iterate over a string, which automatically recognizes Unicode-encoded characters according to the UTF-8 rule.
For pos, char: = Range STR2 {
Fmt. Printf ("character%c starts at byte position%d\ n", char, POS)
}
Output:
Character C starts at byte position 0
Character H starts at byte position 1
Character I starts at byte position 2
Character n starts at byte position 3
Character e starts at byte position 4
Character s starts at byte position 5
Character e starts at byte position 6
Character:starts at byte position 7
Character starts at byte position 8
Character day starts at byte position 9
Character Ben starts at byte position 12
Character language starts at byte position 15
Break and continue these two don't have the same kind of usage.
Label and Goto (label name generally all caps)
Package Mainimport "FMT" Func Main () {LABEL1: for I: = 0, I <= 5; i++ {for J: = 0; J <= 5; j + + = = 4 { continue LABEL1 } fmt. Printf ("I am:%d, and J is:%d\n", I, J)}}}
Package MainFunc Main () { i:=0 here : print (i) i++ if i==5 { return } goto here }
From the touch programming language, it is taught that goto structure is not available, code readability and program structure are very bad
The right to go this book has not been translated, only to the sixth chapter, you have to change the information to see