Go language switch statement usage

Source: Internet
Author: User

Switch is the most flexible type of control statement, which you need to be aware of when using switch:

--The left curly brace "{" must be in the same row as switch

--Conditional expressions are not limited to constants or integers

--Multiple result options can appear in a single case

--contrary to the rules of C, G0 language does not require a break to explicitly exit a case

--only the Fallthrough keyword is explicitly added in the case to continue with the next case immediately following

--You can not set the conditional expression after switch, in which case the entire switch structure is equivalent to the logical action of multiple If....else

--You can have any number of case statements within a switch, each case followed by a value, and a colon

--the expression used in the switch statement must have a whole or boolean expression, or a type, and the default value is true if the expression does not pass

--constant-expression the case, must be the same data type, in the switch variable, it must be a constant or literal

The--switch statement can have an optional default, which must appear at the end of the switch statement, and default can be used to perform a task when no case is true, and no break statement is required after default

Flow chart

650) this.width=650; "Src=" https://s5.51cto.com/wyfs02/M02/05/E3/wKiom1muFNHilokfAAC3ufy45WA934.png-wh_500x0-wm_ 3-wmp_4-s_2298392555.png "title=" Ccc.png "alt=" Wkiom1mufnhilokfaac3ufy45wa934.png-wh_50 "/>

EGG1:

Package Main
Import "FMT"
Func Main () {
var grade string = "A"
Switch {
Case grade = = "A":
Fmt. Printf ("good....\n")
Case grade = = "B":
Fmt. Printf ("Well done....\n")
Case grade = = "C":
Fmt. Printf ("OK ....")
Default
Fmt. Printf ("INVALID grade....\n")
}
Fmt. Printf ("Your grade is%s\n", grade)
}

EGG2:

Package Main
Import "FMT"
Func Main () {
Switch A: = 1; {
Case a >= 0:
Fmt. Printf ("a>=0")

Case a >= 1:
Fmt. Printf ("A=1")
Default
Fmt. Printf ("Not Found ...")
}
}

# #需要注意的是跟在switch属于局部的变量, Access can only be implemented internally, to global must be outside of the switch

This article is from the "Dbaspace" blog, make sure to keep this source http://dbaspace.blog.51cto.com/6873717/1962851

Go language switch statement usage

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.