Go Language selection Statement switch case

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

Depending on the incoming condition, the SELECT statement executes a different statement. The following example prints a different content based on the input integer variable i:

Switch I {case     0:         fmt. Printf ("0") Case     1:         FMT. Printf ("1") Case     2:         fallthrough case     3:         FMT. Printf ("3") Case     4, 5, 6:         FMT. Printf ("4, 5, 6")     default:         

Running the above case, you will get the following results:
i = 0 o'clock, output 0;
i = 1 o'clock, output 1;
i = 2 o'clock, output 3;

i = 3 o'clock, output 3;

i = 4 o'clock, output 4, 5, 6;
i = 5 o'clock, output 4, 5, 6;
i = 6 o'clock, output 4, 5, 6;
i = Any other value, the output is default.

Interestingly, the expression behind switch is not even required, such as the following example:

Switch {case     0 <= num && num <= 3:         FMT. Printf ("0-3") case     4 <= num && num <= 6:         FMT. Printf ("4-6") Case     7 <= num && num <= 9:         FMT. Printf ("7-9")//http://www.cnblogs.com/osfipin/}  

When using the switch structure, we need to pay attention to the following points:
Left Curly brace {must be in the same row as switch;
A conditional expression is not limited to a constant or an integer;
Multiple result options can appear in a single case;
Contrary to the rules of C, the go language does not need to use a break to explicitly exit a case;
The next case will be executed only if the Fallthrough keyword is explicitly added to the case;
You can not set the conditional expression after switch, in which case the entire switch structure with multiple if...else ... The logical function of the same.

Related Article

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.