Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

The switch Statement in Go

The switch Statement in Go

Go Language Conditional Statements Go Language Conditional Statements

The switch statement is used to execute different actions based on different conditions; each case branch is unique and tested one by one from top to bottom until a match is found.

The switch statement executes from top to bottom until a match is found, and no break is needed after the matching item.

By default, switch has an implicit break at the end of each case; after a successful match, no other cases are executed. If we need to execute subsequent cases, we can use fallthrough.

Syntax

The syntax of the switch statement in the Go programming language is as follows:



switch var1 {

    case val1:

        ...

    case val2:

        ...

    default:

        ...

}

The variable var1 can be of any type, while val1 and val2 can be any values of the same type. The type is not restricted to constants or integers, but must be the same type; or expressions whose final result is the same type.

You can test multiple values that may satisfy a condition at the same time, separating them with commas, e.g., case val1, val2, val3.

Flow diagram:

Example

package

main

import

"fmt"

func

main

()

{

   

/* Define local variables */

   

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

(

"Good\n"

)

     

     

case

grade

==

"D"

:

         fmt

.

Printf

(

"Pass\n"

)

     

     

case

grade

==

"F"

:

         fmt

.

Printf

(

"Fail\n"

)

     

default

:

         fmt

.

Printf

(

"Poor\n"

);

   

}

   fmt

.

Printf

(

"your grade is %s\n"

,

grade

);

     

}

The execution result of the code above is:



优秀!

你的等级是 A


Type Switch

The switch statement can also be used in a type-switch to determine the actual type of value stored in an interface variable.

The syntax of a Type Switch is as follows:



switch x.(type){

    case type:

       statement(s);      

    case type:

       statement(s); 

    /* You can define any number of cases */

    default: /* 可选 */

       statement(s);

}

Example

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 of type int"

)

                     

     

case

float64

:

         fmt

.

Printf

(

"x is of type float64"

)

         

     

case

func

(

int

)

float64

:

         fmt

.

Printf

(

"x is of type func(int)"

)

                     

     

case

bool

,

string

:

         fmt

.

Printf

(

"x is of type bool or string"

)

     

     

default

:

         fmt

.

Printf

(

"unknown type"

)

   

   

}

 

}

The execution result of the code above is:



x 的类型 :

fallthrough

Using fallthrough forcibly executes the following case statement; fallthrough does not check whether the next case's expression evaluates to true.

Example

package

main

import

"fmt"

func

main

()

{

   

switch

{

   

case

false

:

            fmt

.

Println

(

"1. case condition is false"

)

           

fallthrough

   

case

true

:

            fmt

.

Println

(

"2. case condition is true"

)

           

fallthrough

   

case

false

:

            fmt

.

Println

(

"3. case condition is false"

)

           

fallthrough

   

case

true

:

            fmt

.

Println

(

"4. case condition is true"

)

   

case

false

:

            fmt

.

Println

(

"5. case condition is false"

)

           

fallthrough

   

default

:

            fmt

.

Println

(

"6. default case"

)

   

}

}

The execution result of the code above is:

2、case 条件语句为 true

3、case 条件语句为 false

4、case 条件语句为 true

From the output of the code above, we can see that switch starts executing from the first case whose expression evaluates to true; if a case has fallthrough, the program continues executing the next case without checking whether the next case's expression is true.

Go Language Conditional Statements Go Language Conditional Statements