Go: control statements

Go: control statements

In this article we will talk about the control statements of the Go programming language.

In this article we will talk about the control statements of the Go programming language.

If statement

The "if" statement in Go executes a block of code if a given boolean expression is true. The basic syntax of an "if" statement is as follows:

if boolean_expression {
     // It is true
}

If the boolean expression is true, the code inside the curly braces will be executed. If the boolean expression is false, the code inside the curly braces will be ignored.

The following is an example of an "if" statement in Go:

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

The code above prints "x is greater than 5" because the variable "x" is set to 10, which is greater than 5.

If-else statement

You can use the "if" statement together with the "else" statement to execute one block of code if the boolean expression is true and another block of code if the boolean expression is false. The basic syntax of an "if-else" statement is as follows:

if boolean_expression {
     // It is true
} else {
     // Is false
}

If the boolean expression is true, the code inside the curly braces after the "if" statement will be executed. If the boolean expression is false, the code inside the curly braces after the "else" statement will be executed.

The following is an example of an "if-else" statement in Go:

package main
func main() {
     x := 10
     if x > 5 {
         fmt.Println("x is greater than 5")
     } else {
         fmt.Println("x is less than 5")
     }
}

The code above prints "x is greater than 5" because the variable "x" is set to 10, which is greater than 5.

If-else-if statement

You can use the "if" statement in conjunction with the "else-if" statement to execute one block of code if the boolean expression is true and another block of code if the boolean expression is false. The basic syntax of an "if-else-if" statement is as follows:

if boolean_expression_1 {
     // It is true
} else if boolean_expression_2 {
     // It is true
} else {
     // Is false
}

If the boolean expression 1 is true, the code inside the curly braces after the "if" statement will be executed. If boolean expression 1 is false and boolean expression 2 is true, the code inside the curly braces after the "else-if" statement will be executed. If Boolean expression 1 and Boolean expression 2 are false, the code inside the curly braces after the "else" statement will be executed.

The following is an example of an "if-else-if" statement in Go:

package main
func main() {
     x := 10
     if x > 5 {
         fmt.Println("x is greater than 5")
     } else if x > 0 {
         fmt.Println("x is greater than 0")
     } else {
         fmt.Println("x is less than 0")
     }
}

The code above prints "x is greater than 5" because the variable "x" is set to 10, which is greater than 5.

It is important to note that the order of the conditions is significant in the "if-else if-else" construct. The conditions are evaluated in sequence and execution occurs in the first block of code that matches the true condition. If none of the conditions are true, the final "else" block is executed.

Switch construct

The switch in Go construct is a powerful and flexible form of multiple selection. It can be used to compare the value of a variable or expression against a set of possible cases and execute the block of code corresponding to the correct case. It's a great alternative to using many nested if-else blocks, especially when you need to make comparisons on multiple values.

The "switch" construct is very similar to that found in other programming languages, such as C, C++, and Java. The syntax is as follows:


     switch value {
         case case1:
           // code to execute for case1
         case case2:
           // code to execute for case2
         case case3:
           // code to execute for case3
         defaults:
           // code to execute if no previous case matches
       }

When the switch construct is executed, the value is compared to the various cases. If one of the cases matches, the corresponding block of code is executed. If no case matches, the code block inside the "default" clause (if any) is executed. The "default" clause is optional and is used when not empty le perform any specific action in case no previous cases matched.

It is important to note that the switch construct in Go can be used with many different types of values, including numbers, strings, custom types, and more. Additionally, expressions can be used as input to the switch construct, allowing for greater flexibility in making decisions.

Another interesting aspect of the switch in Go construct is the ability to use multiple cases for the same block of code. For example, if you want to run the same code for two or more cases, you can group them as shown below:


     switch value {
         case case1, case2:
           // code to execute for case1 and case2
         case case3:
           // code to execute for case3
         defaults:
           // code to execute if no previous case matches
       }

Furthermore, Go also allows you to use the switch construct without specifying a value. In this case, the first case that is true will be executed. You can use this form when you want to carry out more complex checks using Boolean expressions.

The switch in Go construct can be a powerful way to make decisions in code, improving readability and reducing complexity. Compared to nested if-else blocks, the switch construct offers a clearer and more elegant syntax, especially when dealing with multiple values or cases.