The middle loop of go language

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

There are only a few control structures in the go language, and it does not have a while or a do-while loop.

But it has a for, switch, if. and switch accepts an optional initialization statement like for. Let's meet them here.

One, if statement

The IF in the go language looks like this:

If x > 0 {    returnElsereturn x}    

Generally do not need to add parentheses, however, if you write, but also no problem (pro-test, write the parentheses can also ~ ~). Like what:

if (3>2) {        fmt. PRINTLN ("Test If")}true {fmt. PRINTLN ("Test else If")}else{fmt. PRINTLN ("Test Else")}// output test if         

Second, switch statement

The switch of Go is very flexible. An expression does not have to be a constant or an integer.
In Java, the parentheses behind the switch can only put the value of the int type, note that only the int type can be placed, but it is also possible to put the Byte,short,char type,

That's because Byte,short,shar can automatically ascend (automatic type conversion) to int, that is, you put the Byte,short,shar type, and then they automatically convert to int type (wide, auto-convert and secure),
In fact, the last place is the int type!

The switch statement executes the process from top to bottom until a match is found, and there is no need to add a break after the match (again, not the same as Java!). )

Func switchfuncstring (a string) {//StringSwitchACase "Test1": FMT. Println ("Test1")Case "Test2", "Test3": FMT. Println ("Testohter")Default: FMT. Println ("Notest")}}func Switchfuncint (AInt) {//DigitalSwitchACase 1: FMT. Println ("1")Case 2: FMT. Println ("2") case 3: FMT. Println ("3" )}}func Switchfuncbool (c byte//switch? It will match true switch {case " 0 ' <= c && c <= ' 9 ' : FMT. Println (C-' 0 ' ) case ' a ' <= C && c <= ' F ' Span style= "color: #000000;" >: FMT. Println (C-' a ' + 10) case ' a ' <= C && c <= ' F ' : FMT. Println (C-' A ' + 10        

But what if you want to match and then continue to match the following one? There is still a way to use "Fallthrough" can be, for example:

int) {    // digital    switch case1: FMT. Println ("1" Case2: FMT. Println ("2" Case3: FMT. Println ("3")}}          

Call Switchfuncint (1) and print out 1 and secondly.

Third, for Loop

The go language for circulating oil 3 forms, only one of which uses semicolons.

    1. For Init; Condition Post {} and C for the same
    2. For condition {} and while
    3. for {} and C for (;;) (dead Loop)

directly on the code ~ ~

PackageMainImport "FMT"Func Main () {simplefor () var test string = "ASDFGHJKL"Fmt. PRINTLN (reverse ([]Byte// print 0~9func simplefor {for I: = 0; i < i++ {FMT. Println (i)}}// Reverse Afunc Reverse (A []byte) string {//// If you want to execute multiple variables in for, Parallel assignments should be used. for I, J: = 0, Len (a)-1; i < J; I, j = i+1, J-1 {A[i], a[j ] = A[j], A[i]} return              

Branches, loops are very basic things, so it is necessary to practice practiced hand ~ ~

Here are two topics to play ~ ~

1. Create a simple loop based on for. Make it loop 10 times, and print out the value of the counter using the FMT package.
2. Rewrite the 1 loop with Goto. Reserved word for is not available.
3. Rewrite the loop again so that it iterates through an array and prints the array to the screen.

Code:

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.