There are also byte and rune two ways to iterate through a string with a For loop. The first trial was byte and the second rune.
The code is as follows |
Copy Code |
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
The code is as follows |
Copy Code |
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.
The code is as follows |
Copy Code |
Package Main Import "FMT" Func Main () { Sum: = 1 for; Sum < 1000; { sum = Sum } Fmt. PRINTLN (SUM) } |
Based on this, you can omit semicolons:
The code is as follows |
Copy Code |
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.
The code is as follows |
Copy Code |
Package Main Func Main () { for;; { } } |
To avoid redundancy, semicolons can be omitted, so a dead loop can be expressed succinctly.
code is as follows |
copy code |
Package Main Func main () { for { } } |