The switch Statement in Go
When we use switch
in Go, we’re evaluating a condition and defining different outcomes for each case. It’s like writing multiple if-else
statements using the same variable as the condition.
Let’s try a simple example:
day := "monday" switch day { case "monday": fmt.Println("es lunes") case "tuesday": fmt.Println("es martes") case "wednesday": fmt.Println("es miercoles") case "thursday": fmt.Println("es jueves") case "friday": fmt.Println("es viernes") case "saturday": fmt.Println("es sabado") case "sunday": fmt.Println("es sunday") default: fmt.Println("es sunday") }
es lunes
Notice that this switch
isn’t the same as switch
statements in other programming languages. For example, in PHP, the switch would continue printing “It’s Monday,” “It’s Tuesday,” and so on unless you explicitly use break
. In Go, we didn’t have to use break
at all.
But if you do want the switch
to continue executing the following cases, you can use:
fallthrough
Using the previous example, let’s add a fallthrough
inside the "monday"
case:
day := "monday" switch day { case "monday": fmt.Println("is monday") fallthrough case "tuesday": fmt.Println("is tuesday") case "wednesday": fmt.Println("is wednesday") case "thursday": fmt.Println("is thursday") case "friday": fmt.Println("is friday") case "saturday": fmt.Println("is saturday") case "sunday": fmt.Println("is sunday") default: fmt.Println("is sunday") }
es lunes es martes
We may check the OS with switch
switch os := runtime.GOOS; os { case "darwin": fmt.Println("OS X.") case "linux": fmt.Println("Linux.") default: // freebsd, openbsd, // plan9, windows... fmt.Printf("%s.\n", os) }
With this short snippet, you can check which operating system you’re on—a very handy piece of code if you are programming for desktop
check the code in https://play.golang.org/p/IHx1N8Vj7j3
Using switch
for comparisons in Go
Another useful feature of switch
in Go is that it can be used for comparison-based logic. In certain situations, it’s more convenient to use switch
instead of multiple if
, else if
, and else
statements.
package main import ( "fmt" ) func classifyAge(age int) { switch { case age < 13: fmt.Printf("Age %d: Child\n", age) case age >= 13 && age < 18: fmt.Printf("Age %d: Teenager\n", age) case age >= 18 && age < 60: fmt.Printf("Age %d: Adult\n", age) default: fmt.Printf("Age %d: Senior\n", age) } } func main() { classifyAge(10) classifyAge(15) classifyAge(30) classifyAge(70) }
This method of comparison is not posible via switch in some other languages.
Type Switch
Type switch
isn't something you'll use very often—but it exists, so we might as well cover it.
Imagine you need to know the type of a variable you're receiving. In that case, you can use a type switch to take an action depending on what type it is.
Go provides a simple example of how this works in its official Tour
package main import "fmt" func do(i interface{}) { switch v := i.(type) { case int: fmt.Printf("Twice %v is %v\n", v, v*2) case string: fmt.Printf("%q is %v bytes long\n", v, len(v)) default: fmt.Printf("I don't know about type %T!\n", v) } } func main() { do(21) do("hello") do(true) }
You’ll notice that the case
values here are common types like int
, string
, and bool
. You can also match pointers to those types, or even custom types and their pointers.
Keep in mind:
📌 You’ll need to use interface{}
as the input type, because Go checks the concrete type stored inside the interface.