Go Conditions

In Go, conditions are used to make decisions and control the flow of execution based on certain conditions. Conditions are typically expressed using if statements and can be combined with else and else if clauses to create more complex decision-making structures. Here are some examples of conditions in Go:

  1. Simple if statement:
   age := 18
   if age >= 18 {
       fmt.Println("You are an adult")
   }
  1. if-else statement:
   temperature := 25
   if temperature > 30 {
       fmt.Println("It's hot outside")
   } else {
       fmt.Println("It's not too hot")
   }
  1. if-else if-else statement:
   score := 85
   if score >= 90 {
       fmt.Println("A")
   } else if score >= 80 {
       fmt.Println("B")
   } else if score >= 70 {
       fmt.Println("C")
   } else {
       fmt.Println("D")
   }
  1. Nested if statement:
   age := 18
   if age >= 18 {
       fmt.Println("You are eligible to vote")
       if age >= 21 {
           fmt.Println("You are also eligible to drink")
       }
   }

Conditions allow you to check whether certain conditions are true or false and execute different blocks of code accordingly. They are fundamental in implementing decision-making logic in your Go programs and controlling the behavior of your code based on specific conditions.

Go – Decision Making

In Go, decision making is implemented using conditional statements. The conditional statements allow you to make decisions based on certain conditions and control the flow of execution in your program. There are primarily three types of conditional statements in Go: if statements, switch statements, and select statements. Let’s take a look at each of them:

  1. If statements: The if statement is used to execute a block of code if a certain condition is true. It has the following syntax:
   if condition {
       // Code to be executed if the condition is true
   }

Example:

   age := 18
   if age >= 18 {
       fmt.Println("You are an adult")
   }
  1. Switch statements: The switch statement allows you to choose one of several code blocks to be executed based on the value of an expression. It has the following syntax:
   switch expression {
   case value1:
       // Code to be executed if expression equals value1
   case value2:
       // Code to be executed if expression equals value2
   default:
       // Code to be executed if expression doesn't match any case
   }

Example:

   day := "Sunday"
   switch day {
   case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday":
       fmt.Println("It's a weekday")
   case "Saturday", "Sunday":
       fmt.Println("It's a weekend")
   default:
       fmt.Println("Invalid day")
   }
  1. Select statements: The select statement is used to choose one of several communication operations that are ready to proceed. It is used with channels and is similar to a switch statement. It has the following syntax:
   select {
   case channel1 <- value1:
       // Code to be executed if the value is sent to channel1
   case value2 := <-channel2:
       // Code to be executed if a value is received from channel2
   default:
       // Code to be executed if no channel operation is ready
   }

Example:

   ch := make(chan int)
   select {
   case ch <- 10:
       fmt.Println("Value sent to channel")
   case value := <-ch:
       fmt.Println("Value received from channel:", value)
   default:
       fmt.Println("No channel operation ready")
   }

These decision-making constructs allow you to control the flow of your program based on conditions, making it more flexible and responsive to different scenarios. You can choose the appropriate conditional statement based on the requirements of your program.