Skip to main content
โšก Calmops

Conditional Statements in Go

Conditional Statements in Go

Conditional statements control program flow based on conditions. Go provides if/else and switch statements for decision-making.

If/Else Statements

Basic If/Else

package main

import "fmt"

func main() {
    age := 18

    if age >= 18 {
        fmt.Println("Adult")
    } else {
        fmt.Println("Minor")
    }
}

If/Else If/Else

package main

import "fmt"

func main() {
    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("F")
    }
}

If with Initialization

package main

import "fmt"

func main() {
    // Variable declared in if statement
    if x := 10; x > 5 {
        fmt.Println("x is greater than 5")
    }
    // x is not accessible here
}

Switch Statements

Basic Switch

package main

import "fmt"

func main() {
    day := 3

    switch day {
    case 1:
        fmt.Println("Monday")
    case 2:
        fmt.Println("Tuesday")
    case 3:
        fmt.Println("Wednesday")
    default:
        fmt.Println("Unknown day")
    }
}

Switch with Multiple Cases

package main

import "fmt"

func main() {
    day := "Saturday"

    switch day {
    case "Saturday", "Sunday":
        fmt.Println("Weekend")
    case "Monday", "Tuesday", "Wednesday", "Thursday", "Friday":
        fmt.Println("Weekday")
    default:
        fmt.Println("Unknown day")
    }
}

Switch with Initialization

package main

import "fmt"

func main() {
    switch x := 10; {
    case x < 0:
        fmt.Println("Negative")
    case x == 0:
        fmt.Println("Zero")
    case x > 0:
        fmt.Println("Positive")
    }
}

Switch with Fallthrough

package main

import "fmt"

func main() {
    x := 2

    switch x {
    case 1:
        fmt.Println("One")
        fallthrough
    case 2:
        fmt.Println("Two")
        fallthrough
    case 3:
        fmt.Println("Three")
    default:
        fmt.Println("Other")
    }
    // Output:
    // Two
    // Three
}

Type Switches

package main

import "fmt"

func describe(i interface{}) {
    switch v := i.(type) {
    case int:
        fmt.Printf("Integer: %d\n", v)
    case string:
        fmt.Printf("String: %s\n", v)
    case bool:
        fmt.Printf("Boolean: %v\n", v)
    default:
        fmt.Printf("Unknown type: %T\n", v)
    }
}

func main() {
    describe(42)
    describe("hello")
    describe(true)
    describe(3.14)
}

Comparison Operators

package main

import "fmt"

func main() {
    x, y := 10, 20

    fmt.Println(x == y)  // false
    fmt.Println(x != y)  // true
    fmt.Println(x < y)   // true
    fmt.Println(x > y)   // false
    fmt.Println(x <= y)  // true
    fmt.Println(x >= y)  // false
}

Logical Operators

package main

import "fmt"

func main() {
    x, y := true, false

    fmt.Println(x && y)  // false (AND)
    fmt.Println(x || y)  // true (OR)
    fmt.Println(!x)      // false (NOT)

    // Short-circuit evaluation
    if x && someExpensiveFunction() {
        // someExpensiveFunction() only called if x is true
    }
}

func someExpensiveFunction() bool {
    return true
}

Best Practices

โœ… Good: Clear Conditions

// DO: Use clear, readable conditions
if age >= 18 && hasLicense {
    fmt.Println("Can drive")
}

โŒ Bad: Complex Nested Conditions

// DON'T: Deeply nested conditions
if x > 0 {
    if y > 0 {
        if z > 0 {
            // ...
        }
    }
}

โœ… Good: Early Returns

// DO: Use early returns to reduce nesting
func validate(x int) error {
    if x < 0 {
        return fmt.Errorf("x must be positive")
    }
    if x > 100 {
        return fmt.Errorf("x must be <= 100")
    }
    return nil
}

โœ… Good: Switch Over If/Else

// DO: Use switch for multiple cases
switch status {
case "active":
    // ...
case "inactive":
    // ...
default:
    // ...
}

Summary

Go’s conditional statements provide:

  1. If/else for binary decisions
  2. Switch for multiple cases
  3. Type switches for type checking
  4. Logical operators for complex conditions
  5. Early returns to reduce nesting

These tools enable clear, readable control flow in Go programs.

Comments