This is a creation in Article, where the information may have evolved or changed. if: The syntax used is as follows: if x > ten { FMT. PRINTLN ("x greater than ten")
} else { FMT. Println ("x less than ten")
} parentheses are not required in conditional judgment statements. Unlike other languages, the conditional judgment statement allows a variable to be declared, the scope of the variable is valid within the conditional logical block, and other places do not work, such as: if x: = Randval (); x > Ten { FMT. PRINTLN ("x greater than ten")
} else { FMT. Println ("x less than ten")
}
Switch: Syntax: switch expression { Case expression 1:
Program Action Statements
Case expression 2:
Program Action Statements
} switch in Go is very powerful, the value of case does not have to be scalar, but can be the result of an expression, as long as the result type of the expression and the type of the condition is the same, you can set multiple values in a case, As long as the value of the conditional expression has one of the values in this case to execute the statement in the case, and if the box defaults to a break, the success of the match will not execute the following judgment statement, You can add Fallthrough to the case to enforce the following case code. If switch does not have an expression, it will match true. such as; I: = Ten switch I { Case 2-1:
FMT. Println ("I's value is 2-1")
Case 2,3,4,5:
FMT. Println ("I's value is between 2 and 5")
Fallthrough
Case Ten:
FMT. Println ("I's Value equals ten")
Default
FMT. Printf ("I's value is:%d\n", i); }
For : In other languages, it is used to iterate through the data, but he is more powerful, and there are no statements such as while,do while in go that are implemented through a for statement. use: for expression 1; expression 2; expression 3 { Execution of the program statement
} The for judgment statement is also not enclosed in parentheses, as in other languages expression 1 is called before the loop begins, and the expression 3 is called at the end of each cycle, and form 2 is used for each cycle of judgment. In go, expression 1 and expression 3 can be omitted, as the while function such as: sum: = 1 For sum < { sum + = Sum
}
the For Range statement is quite useful like a foreach or for-in statement. You can use a for range to read strings, arrays, slice,map,channel data, such as: for k, V: = Range map{ FMT. PRINTLN ("Map ' s key:", K)
Fmt. PRINTLN ("Map ' s value:", V)
} If you do not need the associated value, you can discard the associated return value by using "_". such as: for _, V: = range arr { .....
} Note: The iterative right expression is calculated once before the loop starts, the iteration variable follows the assignment principle of the variable type, and if the pointer is to a type, then in the iterative process, if there is a modification of the relevant value, it will affect the relevant iterative process, if the value is copied, the original variable is changed during the iteration, Does not affect the iterative process of the value of the pre-loop variable.
Break|continue|goto: As with other languages, break out of the loop, continue to skip this cycle, and break and continue can also follow the label to jump to the out loop in multiple loops. Break can also be used with the label to jump out to the specified label position. Goto is also used with tags to make the program jump to the specified label position, the label starts from the new line, and the label name is case sensitive when the colon ends.
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