Skip to main content

Conditional Statements in Go: If, Else, Switch, Select

In programming, control structures are the decision-making units that manage the flow of a program based on certain conditions. Golang, also known as Go, supports various types of control structures such as if, else, switch, and select. This tutorial will guide you on how to use these control structures in Go.

Conditional Statements in Go

If Statement

An if statement is used to test a condition. If the condition is true, the block of code inside the if statement will be executed.

x := 10
if x > 5 {
fmt.Println("x is greater than 5")
}

In the above example, the condition checks if x is greater than 5. Since x is 10, the condition is true, and the string "x is greater than 5" is printed.

Else Statement

The else statement is used along with an if statement. The block of code inside else is executed if the condition in the if statement is false.

x := 3
if x > 5 {
fmt.Println("x is greater than 5")
} else {
fmt.Println("x is less than or equal to 5")
}

In this example, the if condition is false because x is not greater than 5. Hence, the program prints "x is less than or equal to 5".

Else If Statement

The else if statement is used to include multiple conditions. It checks the conditions from top to bottom until it finds a true condition, then executes the corresponding block of code.

x := 5
if x > 5 {
fmt.Println("x is greater than 5")
} else if x < 5 {
fmt.Println("x is less than 5")
} else {
fmt.Println("x is equal to 5")
}

In the above code, x is equal to 5. So, the program prints "x is equal to 5".

Switch Statement

The switch statement is used when you want to compare a value against multiple possibilities. Unlike if and else, which can only test conditions, switch can test conditions as well as values.

day := "Friday"
switch day {
case "Monday":
fmt.Println("Today is Monday")
case "Tuesday":
fmt.Println("Today is Tuesday")
case "Wednesday":
fmt.Println("Today is Wednesday")
case "Thursday":
fmt.Println("Today is Thursday")
case "Friday":
fmt.Println("Today is Friday")
default:
fmt.Println("Invalid day")
}

In this example, the switch statement checks the value of day. Since day is "Friday", it prints "Today is Friday".

Select Statement

The select statement is a control structure that handles multiple channels in Go. It's similar to switch, but each case statement will be a channel operation.

func main() {
output1 := make(chan string)
output2 := make(chan string)

go func() { output1 <- "from output1" }()
go func() { output2 <- "from output2" }()

select {
case op1 := <-output1:
fmt.Println(op1)
case op2 := <-output2:
fmt.Println(op2)
}
}

In the above code, select waits until one of the case can run, then it executes that case. It chooses randomly if multiple case are ready.

Conclusion

Control structures in Go allow us to control the flow of our program. They help us make decisions in our code based on conditions or values. Understanding and using these control structures effectively can greatly improve your Go programming skills.