Example of a For loop statement in Golang language

Source: Internet
Author: User


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 {
   }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.