Go if statement

In Go, the if statement is used to execute a block of code if a certain condition is true. It allows you to make decisions and control the flow of execution in your program. Here’s the syntax of the if statement in Go:

if condition {
    // Code to be executed if the condition is true

Here are a few examples of using the if statement in Go:

  1. Checking if a number is positive:
   num := 10
   if num > 0 {
       fmt.Println("The number is positive")
  1. Checking if a number is even or odd:
   num := 15
   if num%2 == 0 {
       fmt.Println("The number is even")
   } else {
       fmt.Println("The number is odd")
  1. Checking if a string is empty:
   str := "Hello"
   if str == "" {
       fmt.Println("The string is empty")
   } else {
       fmt.Println("The string is not empty")
  1. Checking multiple conditions using logical operators:
   age := 25
   isStudent := true

   if age >= 18 && isStudent {
       fmt.Println("You are an adult student")

The if statement allows you to evaluate a condition and execute code based on whether the condition is true or false. You can also use the if statement in conjunction with else and else if clauses to create more complex decision-making structures.