Basic syntax
When talking about if-else, it has been mentioned that if there are multiple judgment conditions, the Go language provides a Switch-Case method. If the switch is not followed by the condition, it is equivalent to the switch true
// Convert hexadecimal character to an int value switch { case '0' <= c && c <= '9': return c - '0' case 'a' <= c && c <= 'f': return c - 'a' + 10 case 'A' <= c && c <= 'F': return c - 'A' + 10 } return 0
Fallthrough usage method
By default, the case will be broken after execution is met. Even if the case meets the conditions, the case will not loop again. If you want to continue execution, you need to add fallthrough.
package main import "fmt" func main() { i := 3 switch i { case i > 0: ("condition 1 triggered") fallthrough case i > 2: ("condition 2 triggered") fallthrough default: ("Default triggered") } }
All cases will be executed
condition 1 triggered
condition 2 triggered
Default triggered
Multi-condition matching
If the same condition is met, the same condition can also be listed in this way, equivalent to or condition
switch i { case 0, 1: f() default: g() }
Determine the interface type
Empty interface
Later we will talk about interfaces. You can judge the type through switch and obtain the real type of the interface.
package main import "fmt" func main() { var value interface{} switch q:= value.(type) { case bool: ("value is of boolean type") case float64: ("value is of float64 type") case int: ("value is of int type") default: ("value is of type: %T", q) } }
In the above example, we define an empty interface
var value interface{}
Use switch to determine the type
switch q:= value.(type) {
Since the empty interface has no content, the type is nil, and the default is triggered
value is of type: <nil>
Get the actual type
We transform the above example, and at the same time let the empty interface have actual values. Let’s take a look at the execution effect
package main import "fmt" func valueType(i interface{}) { switch q:= i.(type) { case bool: ("value is of boolean type") case float64: ("value is of float64 type") case int: ("value is of int type") default: ("value is of type: %T\n", q) } } func main() { person := make(map[string]interface{}, 0) person["name"] = "Alice" person["age"] = 21 person["height"] = 167.64 ("%+v\n", person) for _, value := range person { valueType(value) } }
Here are a few knowledge points that have not been discussed yet:
- Function definition method
- A map is defined, but the value type is an empty interface, which means it can be any type of value. This will be explained in detail in the interface chapter, so don't worry about it when you see this, continue reading
- When assigning values, specialize in giving value different types, string/int/float type
Finally, pass the variable to the valueType function through a loop to see what the program outputs
map[age:21 height:167.64 name:Alice]
value is of type: string
value is of int type
value is of float64 type
This is the article about the use of Switch statements for Go language learning. For more relevant Go language Switch statement content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!