Basic syntax
Syntax format that is the same as C language, with a loop that starts and ends, for init; condition; post { }
while loop with condition, for condition { }
Infinite loop, for { }
A conditional cycle with beginning and end
sum := 0 for i := 0; i < 10; i++ { sum = sum + i }
Note: The i variable cannot be used after the loop ends
Cycle with conditions
count := 0 for count < 10 { ("Current count = %v\n", count) count++ }
Infinite loop
Since the loop will not stop, break is used here to interrupt the loop, which will be introduced in detail later
count := 0 for { ("current count = %v\n", count) count++ if count > 10 { break } }
Array loop
Loop using counter
Similar to loops in C language, we can traverse the array through counters combined with array length, and at the same time get the array index, as shown in the following example
package main import "fmt" func main() { myarray := []string{"a", "b", "c", "d"} for i := 0; i < len(myarray); i++ { ("Array index is %v, value is %v\n", i, myarray[i]) } }
Using range loops
Range can be used to loop more easily, and range can also be used in slice, channel and map loops.
package main import "fmt" func main() { myarray := []string{"a", "b", "c", "d"} for index, item := range myarray { ("current index is %v, value is %v\n", index, item) } }
Map loop
When introducing Map, we have tried to traverse the Map using a for loop, let's consolidate it.
package main import "fmt" func main() { mymap := map[int]string{1 : "a", 2 : "b", 3 : "c"} for key, value := range mymap { ("current key is %v, value is %v\n", key, value) } }
If you just want to get the key, you can use it, omitting the value
for key := range mymap {
Or use _, as mentioned earlier, cannot be used for variables, like a placeholder
for _, value := range mymap {
The traversal of string
The following example is a traversal of the string type. Except for ordinary characters, for Unicode characters, the characters are usually 8-bit, and the characters of UTF-8 may be up to 32-bit.
package main import "fmt" func main() { mystr := "abc" for pos, char := range mystr { ("character '%c' starts at byte position %d\n", char, pos) } for pos, char := range "Gő!" { ("character '%c' starts at byte position %d\n", char, pos) } }
character 'G' starts at byte position 0
character 'ő' starts at byte position 1
character '!' starts at byte position 3
Break and Continue
Consistent with most languages
- Break ends the current loop
- Continue starts the next loop
package main import "fmt" func main() { for i := 0; i < 10; i++ { if i == 3 { ("For continue at here: %d\n", i) continue } if i > 5 { ("For break at here: %d\n", i) break } ("Current for count: %d\n", i) } ("For loop end here") }
Output result
Current for count: 0
Current for count: 1
Current for count: 2
For continue at here: 3
Current for count: 4
Current for count: 5
For break at here: 6
For loop end here
Not recommended
Go also supports Lable method, similar to Goto, generally not used
J: for j := 0; j < 5; j++ { for i := 0; i < 10; i++ { if i > 5 { break J } (i) } }
This is the article about the detailed explanation of the use of for loops, break and continue in Go. For more related Go languages, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!