Golang Tutorial

Fundamentals

Control Statements

Functions & Methods

Structure

Arrays & Slices

String

Pointers

Interfaces

Concurrency

Decision Making Statements in Golang

In programming, decision-making structures allow you to execute specific blocks of code based on certain conditions. Go supports several decision-making statements, and this tutorial will guide you through them.

1. The if Statement

The if statement is the most fundamental decision-making statement.

package main

import "fmt"

func main() {
    x := 20

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

2. The if-else Statement

You can use the else statement to specify a block of code to be executed if the condition in the if statement is false.

x := 5

if x > 10 {
    fmt.Println("x is greater than 10.")
} else {
    fmt.Println("x is less than or equal to 10.")
}

3. The if-else if-else Chain

For multiple conditions, you can chain if-else statements.

x := 15

if x > 20 {
    fmt.Println("x is greater than 20.")
} else if x > 10 {
    fmt.Println("x is greater than 10 but less than or equal to 20.")
} else {
    fmt.Println("x is less than or equal to 10.")
}

4. The switch Statement

The switch statement is used to select one of many blocks of code to be executed.

y := 2

switch y {
case 1:
    fmt.Println("y is 1.")
case 2:
    fmt.Println("y is 2.")
default:
    fmt.Println("y is neither 1 nor 2.")
}

5. The switch Statement with Multiple Expressions

A case can have multiple expressions.

y := 5

switch y {
case 1, 3, 5, 7:
    fmt.Println("y is an odd number less than 10.")
case 2, 4, 6, 8:
    fmt.Println("y is an even number less than 10.")
default:
    fmt.Println("y is 10 or greater.")
}

6. The switch Statement with Initialization

You can have an optional initialization statement in the switch.

switch z := 5 + 3; z {
case 8:
    fmt.Println("5 + 3 equals 8.")
case 9:
    fmt.Println("5 + 3 equals 9.")
}

7. The switch Statement without an Expression

A switch without an expression is an alternate way to express if/else logic.

x := 42

switch {
case x%2 == 0:
    fmt.Println("x is even.")
default:
    fmt.Println("x is odd.")
}

8. The select Statement

Though not precisely a standard decision-making statement, the select statement lets a Go program wait for multiple communication operations. It's used with channels, and it chooses the case that can proceed, making it useful for decision-making in concurrent programming.

ch1 := make(chan string)
ch2 := make(chan string)

go func() {
    ch1 <- "From Channel 1"
}()

go func() {
    ch2 <- "From Channel 2"
}()

select {
case msg1 := <-ch1:
    fmt.Println(msg1)
case msg2 := <-ch2:
    fmt.Println(msg2)
}

Conclusion

This tutorial covered the essential decision-making statements in Go. Mastering these structures will allow you to write dynamic programs that can respond and adjust to different situations.

  1. If Statements in Golang with Examples:

    • The if statement in Golang is used for conditional execution.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          num := 10
      
          // If statement
          if num > 0 {
              fmt.Println("Number is positive")
          }
      }
      
  2. Conditional Statements in Golang Programming:

    • Conditional statements include if, else if, and else.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          num := 0
      
          // Conditional statements
          if num > 0 {
              fmt.Println("Number is positive")
          } else if num < 0 {
              fmt.Println("Number is negative")
          } else {
              fmt.Println("Number is zero")
          }
      }
      
  3. Golang if-else Statements and Usage:

    • if-else statements handle multiple conditions.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          num := 10
      
          // If-else statement
          if num%2 == 0 {
              fmt.Println("Number is even")
          } else {
              fmt.Println("Number is odd")
          }
      }
      
  4. Comparisons and Logical Operators in Golang Conditions:

    • Golang supports standard comparisons and logical operators.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          age := 25
      
          // Comparisons and logical operators
          if age >= 18 && age <= 60 {
              fmt.Println("Age is between 18 and 60")
          }
      }
      
  5. Using Switch-Case for Multiple Conditions in Golang:

    • switch statements handle multiple conditions with case.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          day := "Saturday"
      
          // Switch-case statement
          switch day {
          case "Monday":
              fmt.Println("It's Monday!")
          case "Saturday", "Sunday":
              fmt.Println("It's the weekend!")
          default:
              fmt.Println("It's a regular day")
          }
      }
      
  6. Golang Nested If Statements and Their Structure:

    • Nested if statements are used for deeper conditional checks.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          num := 10
      
          // Nested if statements
          if num > 0 {
              if num%2 == 0 {
                  fmt.Println("Number is positive and even")
              } else {
                  fmt.Println("Number is positive and odd")
              }
          } else {
              fmt.Println("Number is not positive")
          }
      }
      
  7. Ternary Operators in Golang for Decision Making:

    • Golang does not have a ternary operator, but a conditional expression can be used.
    • Example:
      package main
      
      import "fmt"
      
      func main() {
          num := 10
      
          // Conditional expression as a substitute for ternary operator
          result := func() int {
              if num > 0 {
                  return 1
              }
              return 0
          }()
      
          fmt.Println("Result:", result)
      }