Go Fifth day

Source: Internet
Author: User
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


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.