Switch in Go
In this post, you will learn how to work with switch so that you can use that same knowledge and thus evolve.
- the switch is another way of working with decisions and it can only check an equality relationship, that is, it cannot use comparison operators (and of course, note that there are two types of switch (by expression and/or by type) );
- the case compares what was defined in itself with the content of the argument that was defined in the switch;
- default is executed when the content of the argument that was defined in the switch is not found in any case.
package main
import "fmt"
func main() {
_case := "D"
switch (_case) {
case "A":
fmt.Println("The case A exists.")
case "B":
fmt.Println("The case B exists.")
case "C":
fmt.Println("The case C exists.")
default:
fmt.Printf("The case %s does not exist.", _case)
}
}
// The case D does not exist.
Don’t forget to watch the video and you can always read this post in Portuguese.