Deep analysis of the use of switch statements in Go programming _golang

Source: Internet
Author: User
Tags case statement constant switch case

A switch statement allows a variable to be tested equal to the list of opposing values. Each value is referred to as a case, and the variable is switched on to check each switch case.

In go programming, there are two types of switch.

Expression switch-the value of the switch expression when the expression Switch,case contains a comparison.

Type switch-in this type of switch, there is a type that contains a special note switch expression at this time.

Expression switch
the syntax for expressing switch statements in the GO programming language is as follows:

Copy Code code as follows:

Switch (boolean-expression or integral type) {
Case boolean-expression or integral type:
statement (s);
Case boolean-expression or integral type:
statement (s);
/* can have any of the case statements * *
Default:/* Optional * *
statement (s);
}

The following rules apply to a switch statement:

An expression used in a switch statement must have a whole or boolean expression, or a type, wherein the class has a single conversion function, either as a whole or as a Boolean value. If the expression does not pass, the default value is true.

You can have any number of case statements in the switch. Each case is followed by a value that is compared, and a colon.

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

When the variable is connected to the value of the case, the statement is executed in the following case. The break in the case statement is not required.

The switch statement can have an optional default, it must appear at the end of the switch. By default, the case that is available for performing a task is true. The case is not required by default.

Flow chart:

Example:

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
/* local variable definition * *
var grade string = "B"
var marks int = 90

Switch Marks {
Case 90:grade = "A"
Case 80:grade = "B"
Case 50,60,70:grade = "C"
Default:grade = "D"
}

Switch {
Case grade = = "A":
Fmt. Printf ("excellent!\n")
Case grade = = "B", Grade = = "C":
Fmt. Printf ("OK done\n")
Case grade = = "D":
Fmt. Printf ("You passed\n")
Case grade = = "F":
Fmt. Printf ("Better try Again\n")
Default
Fmt. Printf ("Invalid grade\n");
}
Fmt. Printf ("Your grade is%s\n", grade);
}


When the above code is compiled and executed, it produces the following results:

It's done
excellent!
. Your grade is A

Type switch
The syntax for a type switch statement in the Go programming language is as follows:

Copy Code code as follows:

Switch X. (type) {
Case Type:
statement (s);
Case Type:
statement (s);
/* can have any of the case statements * *
Default:/* Optional * *
statement (s);
}

The following rules apply to a switch statement:

Use variable Expression {} input that must have an interface in the switch statement.

There can be any number of case statements within the switch. Each case is followed by a comparison of the values and a colon.

The type of case must be the same data type, in the switch variable, it must be a valid data type.

When a variable is connected to a value in a case, the following case statement is executed. The break in the case statement block is not required.

The switch statement can have an optional default case, which must appear at the end of the switch. By default, can be used to perform a task when there is no matching case. Default is not required.

Example:

Copy Code code as follows:

Package Main

Import "FMT"

Func Main () {
var x interface{}

Switch I: = X. (type) {
Case NIL:
Fmt. Printf ("Type of x:%T", i)
Case INT:
Fmt. Printf ("x is int")
Case Float64:
Fmt. Printf ("x is Float64")
case func (int) Float64:
Fmt. Printf ("X is func (int)")
case bool, String:
Fmt. Printf ("X is bool or string")
Default
Fmt. Printf ("Don t know the type")
}
}


Let's compile and run the above program, which will produce the following results:

Type of x:<nil>

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.