GO Learning - switch Branch

1. switch Branch

The switchI statement is used to perform different actions based on different conditions. Each branch of a case is only -, tested from top to bottom until it matches. There is no need to break after a match

Basic grammar:

switch expression {

case expression 1, expression 2,...

Statement block 1

case EXPRESSION 3, EXPRESSION 4,.:

default:

Statement block

}

2. Flowchart

3. Introduction exercises

Write a program that accepts a character, such as a,b.c,d,ef.g a for Monday and B for Tuesday to display dependent information based on user input, requiring a switch statement to complete

//Use of switch
package main
import (
	"fmt"
)
func main(){
	//Define a variable to receive user input
	var key byte
	fmt.Println("Please enter a character")
	fmt.Scanf("%c",&key)
	//Use switch for judgment
	switch key {
		case 'a':
			fmt.Println("Monday")
	    case 'b':
			fmt.Println("Tuesday")
	    case 'c':
			fmt.Println("Wednesday")
	    case 'd':
			fmt.Println("Thursday")
	    case 'e':
			fmt.Println("Friday")
	    case 'f':
			fmt.Println("Saturday")
	    case 'g':
			fmt.Println("Sunday")
		default:
			fmt.Println("Please enter correctly")

	}
}

IV. Attention to Details

1. After case is an expression (i.e., constant value, variable, a function with return value, etc.)

//Use of switch
package main
import (
	"fmt"
)
//Define a function
func test(b byte) byte {
	return b + 1
}
func main(){
	//Define a variable to receive user input
	var key byte
	fmt.Println("Please enter a character")
	fmt.Scanf("%c",&key)
	//Use switch for judgment
	switch key { //variable
		case 'a': 
			fmt.Println("Monday")
	    case test(key): //function
			fmt.Println("Tuesday")
	    case test(key) + 1: //Functional operations
			fmt.Println("Wednesday")
	    case 'd':
			fmt.Println("Thursday")
	    case 'e':
			fmt.Println("Friday")
	    case 'f':
			fmt.Println("Saturday")
	    case 'g':
			fmt.Println("Sunday")
		default:
			fmt.Println("Please enter correctly")

	}
}}

2. The data type of the value of each expression after case must be the same as the expression data type of switch

//Use of switch
package main
import (
	"fmt"
)

func main(){
	var a1 int = 10
	var a2 int8 = 10
	switch a1 { //Error, inconsistent data type
		case a2 :
			fmt.Println("Match Successful")
		default:
			fmt.Println("Matching Failed")
	}
	
}

3. Case can be followed by multiple expressions, using comma intervals. For example, case expression 1, expression 2...

//Use of switch
package main
import (
	"fmt"
)
func test(k int) int {
	return k
}
func main(){
	var a1 int = 20
	var a2 int = 10
	switch a1 { 
		case a2 , 15, test(20):
			fmt.Println("Match Successful")
		default:
			fmt.Println("Matching Failed")
	}
	
}

4. If the expression after case is a constant value (literal quantity), it must not be repeated

//Use of switch
package main
import (
	"fmt"
)
func test(k int) int {
	return k
}
func main(){
	var a1 int = 20
	var a2 int = 10
	switch a1 { 
		case a2 , 15, test(20):
			fmt.Println("Match Successful")
		case 15: //Error, constant repeats, but variable does not
			fmt.Println("Match Passed")	
		default:
			fmt.Println("Matching Failed")
	}
	
}

5. There is no need to break after a case. When the program matches a case, it executes the corresponding code block and exits the switch. If - none matches, default is executed.

6. default statements are not required

//Use of switch
package main
import (
	"fmt"
)
func test(k int) int {
	return k
}
func main(){
	var a1 int = 20
	var a2 int = 10
	switch a1 { 
		case a2 , 15, test(30):
			fmt.Println("Match Successful")
		case 25 :
			fmt.Println("Success")
	}
	
}

7. switch can also be used without expression, similar to if-else branch.

//Use of switch
package main
import (
	"fmt"
)
func test(k int) int {
	return k
}
func main(){
	//Value Judgment
	var a1 int = 20
	switch  { 
		case a1 == 20:
			fmt.Println("Match Successful")
		default:
			fmt.Println("Matching Failed")
	}
	//Judging the scope
	var a2 int = 25
	switch {
		case a2 >= 30 && a2 < 40:
			fmt.Println("ok1")
		case a2 > 20 && a2 < test(40):
			fmt.Println("ok2")
		default:
			fmt.Println("No match")		
	}
	
}

8. A variable can also be declared/defined directly after a switch, ending with a semicolon, which is not recommended.

//Use of switch
package main
import (
	"fmt"
)
func main(){
	//Pairwise break
	switch a1 := 20;{ //Be sure to have a semicolon
		case a1 == 20:
			fmt.Println("Match Successful")
		default:
			fmt.Println("Matching Failed")
	}
}

9. Switch penetrates fllthrough. If allthrough is added after the case block, the next case, also known as switch penetration, will continue.

//Use of switch
package main
import (
	"fmt"
)
func main(){
	//Define a variable to receive user input
	var key byte
	fmt.Println("Please enter a character")
	fmt.Scanf("%c",&key)
	//Use switch for judgment
	switch key {
		case 'a':
			fmt.Println("Monday")
			fallthrough //By default, only one layer is penetrated
	    case 'b':
			fmt.Println("Tuesday")
	    case 'c':
			fmt.Println("Wednesday")
	    case 'd':
			fmt.Println("Thursday")
	    case 'e':
			fmt.Println("Friday")
	    case 'f':
			fmt.Println("Saturday")
	    case 'g':
			fmt.Println("Sunday")
		default:
			fmt.Println("Please enter correctly")

	}
}

10, Type Switch: switch statement can also be used to type switch to determine the type of variable actually pointed to in an interface variable

package main
import (
	"fmt"
)

func main(){
  var x interface{}
  var y = 12.0
  x = y
  switch i := x.(type) {
  	case nil:
		fmt.Println("x Type of~ :%T",i)
	case int:
		fmt.Println("x yes int type")
    case float64:
		fmt.Println("x yes float64")
	case func(int) float64:
		fmt.Println("x yes func(int) type")
	case bool, string:
		fmt.Println("x yes bool perhaps string type")	
	default:
		fmt.Println("No corresponding type")
  }
}

5. Exercise 1

Use switch to convert lowercase char s to uppercase (keyboard input). Only a, b,c, d,e. other output "other"

//Use of switch
package main
import (
	"fmt"
)
func main(){
	//Define a variable
	var a1 byte 
	//Enter on keyboard
	fmt.Println("Please enter any letter")
	fmt.Scanf("%c",&a1)
	//Use switch judgment
	switch a1 {
		case 'a':
			fmt.Printf("%c",'a'-32) //Use ascii code to get uppercase letters
		case 'b':
			fmt.Printf("%c",'b'-32)
		case 'c':
			fmt.Printf("%c",'c'-32)
		case 'd':
			fmt.Printf("%c",'d'-32)
		case 'e':
			fmt.Printf("%c",'e'-32)
		default:
			fmt.Println("other")	
	}
}

Tags: Go

Posted on Sun, 26 Sep 2021 13:28:33 -0400 by TFD3