The example in this article describes the use of a For loop statement in the Golang language. Share to everyone for your reference. The specific analysis is as follows:
A For loop is used to traverse an array or a number. There are also byte and rune two ways to iterate through a string with a For loop. The first is byte and the second is rune.
Copy Code code as follows:
Package Main
Import (
"FMT"
)
Func Main () {
S: = "ABC Kanji"
For I: = 0; I < Len (s); i++ {
Fmt. Printf ("%c,", S[i])
}
Fmt.println ()
For _, R: = Range S {
Fmt. Printf ("%c,", R)
}
}
Output results:
A,b,c,d,e,f,d,ä,¸, the
In ABCDEFD
Comparison
Copy Code code as follows:
Package Main
Import "FMT"
Func Main () {
Sum: = 0
For I: = 0; I < 10; i++ {
sum = i
}
Fmt. PRINTLN (SUM)
}
As in C, you can leave the front and back statements blank.
Copy Code code as follows:
Package Main
Import "FMT"
Func Main () {
Sum: = 1
for; Sum < 1000; {
sum = Sum
}
Fmt. PRINTLN (SUM)
}
Based on this, you can omit semicolons:
Copy Code code as follows:
Package Main
Import "FMT"
Func Main () {
Sum: = 1
For sum < 1000 {
sum = Sum
}
Fmt. PRINTLN (SUM)
}
If the loop condition is omitted, it is a dead loop source.
[Code]package Main
Func Main () {
for;; {
}
}
To avoid redundancy, semicolons can be omitted, so a dead loop can be expressed succinctly.
Copy Code code as follows:
Package Main
Func Main () {
for {
}
}
I hope this article will be helpful to everyone's Golang language programming.