How to Use Conditionals in Golang

In this class, we’ll be covering how you can use conditionals in Golang. So, let’s dive in and find out how to use conditionals in Golang.
 
You might be thinking, “What are conditionals?”
 
Well, conditionals are expressions that result to either true or false when evaluated. The main purpose of using conditionals is to help in controlling the program flow during execution. Two conditionals are employed in Golang. These are:

    •If else statement
    •Switch statement

Let’s dive in and find out how you can use these conditionals.
 
#1: If else statement
In Golang, a simple if statement has the following form:

if Boolean expression
{
Statements
}

Here’s an example of a code that tests if the average score is greater than or equal to 80 and displays grade “A”.

if average>=80
{ 
grade=”A.”
}

An if statement can only be executed when the Boolean expression is true. Otherwise, the statement is skipped. In some cases, if statement can be followed by an else statement that is optional. When you have an optional if statement, the optional statement executes only when the Boolean expression is false. Here’s the syntax:

if Boolean expression
{
Statement(s) /* they can only execute when the Boolean expressions are true*/
   }
else
{
Statement(s) /* they can only execute when the Boolean expressions are false*/
}

Note that the Boolean expression is not enclosed in brackets. Here’s an example of if statement that has optional else statement:

if average>=50 
{
remark=” Passed.”
}
else
{
remark=” Failed.”
}

An if else statement can also be nested. For instance, the Golang code below shows how the if else statement can nested:

if average>=70
{
grade=”A.”
}
else if
average>=60
{
grade=” B.”
}
else if
average>=50
{
grade=” C.”
}
else if
average>=40
{
grade=” D.”
}
else
grade=” F.”
}

#2: The Switch Statement
The switch statement enables a variable to be tested for equivalence against a set of values where each value—commonly referred to as the case—is the variable that’s checked for each switch case. There are two types of defining the switch statement. These are:

    •The expression switch.
    •The type switch.

While the expression switch statement has expressions that are compared against the value for each of the switch expressions, the type switch contains the types that are compared against the type of special annotated switch expressions.
 
The syntax for the expression switch statement is as follows:

switch Boolean expression {
    case Boolean expression:
       Golang statement(s)     
    case Boolean expression
       Golang statement(s); 
    default :
      Golang  statement(s);
}

Note that the Boolean expression can also be substituted with integral value. Here’s an example of a Golang code that uses expression switch:

switch marks {
      case 90: grade = "A"
      case 80: grade = "B"
      case 50, 60, 70: grade = "C"
      Default: grade = "D"  
The syntax for the type switch is as follows:
switch y. (type) {
    case type:
       Golang statement(s);      
    case type:
       Golang statement(s); 
        default: 
      Golang statement(s);
}

Here’s what you should note about the syntax of the type switch statement:

    •The expression that’s used in a type switch statement should have variable of interface {} type.
    •The type of your case should be of the same data type as that of variables in the kind switch—which must be a valid data type.
    •You can have an optional default statement in a type switch statement.

Here’s an example of Golang code that demonstrates use of type switch statement:

var y interface{}
   switch m := y.(type) {
      case int32:	  
         fmt.Printf ("y is integer that has 32 bits")                       
      case float64:
         fmt.Printf ("y is floating point number with 64 bits")           
      case bool, string:
         fmt.Printf ("y is either a Boolean or string")       
      default:
         fmt.Printf("I don't know the type")   

Online Technology Courses Stone River eLearning
GET YOUR FREE Golang EBOOK!

Comments