Go from Beginner to Expert: A Complete Guide to Learn Golang PART-5 Step-by-Step Guide to understand Control Statements(switch-case) of Golang

Go from Beginner to Expert: A Complete Guide to Learn Golang PART-5

Step-by-Step Guide to understand Control Statements(switch-case) of Golang

Md. Faiyaj Zaman
4 min readJan 9, 2023

--

Welcome back to this tutorial on control statements in Golang. In the previous section, we discussed if-else statements and how to use them in your code. In this section, we will be discussing switch statements in Golang.

To understand Control Statements(if-else) of Golang, please check our previous post here.

Switch Statements

Switch statements are another type of control statement in Golang that allows you to specify a set of conditions and the code that should be executed based on those conditions. The switch statement is often used as a shorter form of an if-else statement when there are many conditions to be tested.

1Syntax and examples of using switch statements in Golang

Here is the syntax for a switch statement:

switch expression {

case value1: // code to execute if expression == value1

case value2: // code to execute if expression == value2

… … … … … … … … … … … … … … … … … … …

default: // code to execute if no case matches
}

In a switch statement, the expression is evaluated and compared to the values specified in each case. If a match is found, the code in that case is executed. If no match is found, the code in the default case is executed.

Here is an example of a switch statement in Golang:

package main

import "fmt"

func main() {

x := 10

switch x {

case 1: fmt.Println("x is 1")

case 10: fmt.Println("x is 10)

default: fmt.Println("x is not 1, 10, or 100")

}
}

You can also use multiple values in a case by separating them with a comma.

Here is an example of a switch statement with multiple values in a case:

package main

import "fmt"

func main() {

x := 10

switch x {

case 1, 5, 10:

fmt.Println("x is 1, 5, or 10")

case 100:

fmt.Println("x is 100")

default:

fmt.Println("x is not 1, 5, 10, or 100")

}

}

In this example, the expression being evaluated is x. The value of x is compared to the values specified in each case. In this case, x is equal to 10, so the code in the case 1, 5, 10: block is executed.

2Using the switch statement as a shorter form of an if-else statement

You can also use the switch statement as a shorter form of an if-else statement by omitting the expression and using a boolean value in each case.

Here is an example of a switch statement used as a shorter form of an if-else statement:

package main

import "fmt"

func main() {

x := true

switch {

case x == true:

fmt.Println("x is true")

case x == false:

fmt.Println("x is false")

}

}

In this example, we are using a switch statement to check the value of x. Because we have omitted the expression in the switch statement, the code in the first case block case x == true: will be executed if x is true.

3Fallthrough keyword in Golang’s switch statement

The switch statement in Golang also has a fallthrough keyword that allows you to execute the code in the next case even if the current case does not match the expression.

Here is an example of using the fallthrough keyword in a switch statement:

package main

import "fmt"

func main() {

x := 10

switch x {

case 1:

fmt.Println("x is 1")

fallthrough
case 10:

fmt.Println("x is 10")

case 100:

fmt.Println("x is 100")

default:

fmt.Println("x is not 1, 10, or 100")

}

}

In this example, the expression being evaluated is x. The value of x is compared to the values specified in each case. In this case, x is equal to 10, so the code in the case 1: and case 10: blocks will be executed. Without the fallthrough keyword, only the code in the case 10: block would be executed.

These are just a few examples of how to use switch statements in Golang. As you can see, they are a useful tool for controlling the flow of your code execution based on certain conditions.

In the next section of this tutorial, we will be discussing for loops in Golang. Stay tuned for more on control statements in Golang!

To stay up-to-date on our latest posts, please click on the Follow button and subscribe to our email notifications.

Kindly explore the complete series on Golang, which presents a step-by-step guide, available here.

Happy Coding :)

--

--