Go Switch Statement

List Topics
February 15, 2025
No Comments
4 min read

The switch statement in Go is used for decision-making, similar to if-else, but it is more readable when multiple conditions need to be checked. It allows a variable to be tested against multiple values, executing the corresponding case when a match is found.


1. Basic Syntax of Switch Statement

Go
switch expression {
case value1:
    // Code to execute if expression == value1
case value2:
    // Code to execute if expression == value2
default:
    // Code to execute if no cases match
}

🔹 Key Points:
✅ No break statement is needed (unlike C, C++). Once a matching case executes, it automatically exits the switch block.
✅ default case is optional, but it is used when no other case matches.


2. Basic Example of Switch

Go
package main

import "fmt"

func main() {
    day := "Monday"

    switch day {
    case "Monday":
        fmt.Println("Start of the work week!")
    case "Friday":
        fmt.Println("Weekend is near!")
    default:
        fmt.Println("It's a regular day.")
    }
}

🔹 Output:

Bash
Start of the work week!

👉 Explanation:

  • day == "Monday", so the first case executes.
  • The program exits the switch after that.

3. Multiple Cases in One Line

Go allows multiple cases to share the same block of code.

Go
package main

import "fmt"

func main() {
    day := "Saturday"

    switch day {
    case "Saturday", "Sunday":
        fmt.Println("It's the weekend!")
    default:
        fmt.Println("It's a weekday.")
    }
}

🔹 Output:

Bash
It's the weekend!

👉 Explanation:

  • day == "Saturday", which matches case "Saturday", "Sunday", so the message is printed.

4. Switch Without Expression (Acting Like if-else)

In Go, a switch can work without an expression, making it similar to an if-else ladder.

Go
package main

import "fmt"

func main() {
    num := -5

    switch {
    case num > 0:
        fmt.Println("Positive number")
    case num < 0:
        fmt.Println("Negative number")
    default:
        fmt.Println("Zero")
    }
}

🔹 Output:

Bash
Negative number

👉 Explanation:

  • Since num < 0, the second case executes.

5. Fallthrough in Go (Forcing Execution of Next Case)

By default, Go does not fall through cases like C/C++. However, you can force execution of the next case using the fallthrough keyword.

Go
package main

import "fmt"

func main() {
    num := 1

    switch num {
    case 1:
        fmt.Println("Case 1 executed")
        fallthrough
    case 2:
        fmt.Println("Case 2 executed")
    default:
        fmt.Println("Default case")
    }
}

🔹 Output:

Bash
Case 1 executed
Case 2 executed

👉 Explanation:

  • num == 1, so case 1 executes.
  • fallthrough forces case 2 to execute as well, even though num is not 2.

6. Type Switch (Checking Variable Type in Interface{})

A type switch is used when you want to check the type of a variable stored in an interface{}.

Go
package main

import "fmt"

func typeCheck(i interface{}) {
    switch v := i.(type) {
    case int:
        fmt.Println("It's an integer:", v)
    case string:
        fmt.Println("It's a string:", v)
    default:
        fmt.Println("Unknown type")
    }
}

func main() {
    typeCheck(10)
    typeCheck("GoLang")
    typeCheck(3.14)
}

🔹 Output:

Bash
It's an integer: 10
It's a string: GoLang
Unknown type

👉 Explanation:

  • The function checks the type of i and executes the corresponding case.
  • int and string are matched, but 3.14 (float) falls into default.

7. Comparing Switch vs. If-Else

FeatureSwitchIf-Else
ReadabilityBetter for multiple conditionsBecomes lengthy with many conditions
PerformanceFaster (compiles to jump table)Can be slower for many conditions
Type CheckingCan be used for type switchesRequires explicit type checks
FallthroughDisabled by defaultNot applicable

Conclusion

✅ switch is more readable than multiple if-else statements.
✅ No break is needed (it exits automatically).
✅ You can have multiple values in one case.
✅ Type switches allow checking variable types.
✅ fallthrough forces execution of the next case.

🚀 Use switch for cleaner and faster conditional checks!


āĻŦāĻžāĻ‚āϞāĻž āĻŦā§āϝāĻžāĻ–ā§āϝāĻž

Go-āĻāϰ Switch āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āϟ āϕ⧀?

switch āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āϟ āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧇ āĻāĻ•āϟāĻŋ āϭ⧇āϰāĻŋāϝāĻŧ⧇āĻŦāϞ āĻŦāĻŋāĻ­āĻŋāĻ¨ā§āύ āĻŽāĻžāύ⧇āϰ āϏāĻžāĻĨ⧇ āϤ⧁āϞāύāĻž āĻ•āϰ⧇ āύāĻŋāĻ°ā§āĻĻāĻŋāĻˇā§āϟ āϕ⧋āĻĄ āĻŦā§āϞāĻ• āϚāĻžāϞāĻžāύ⧋ āϝāĻžāϝāĻŧāĨ¤ āĻāϟāĻŋ if-else-āĻāϰ āĻšā§‡āϝāĻŧ⧇ āϏāĻšāϜ āĻ“ āĻĻā§āϰ⧁āϤāϤāϰ āϏāĻŽāĻžāϧāĻžāύāĨ¤


1. Switch-āĻāϰ āĻŦ⧇āϏāĻŋāĻ• āĻ—āĻ āύ

Go
switch expression {
case value1:
    // āϝāĻĻāĻŋ expression == value1 āĻšāϝāĻŧ
case value2:
    // āϝāĻĻāĻŋ expression == value2 āĻšāϝāĻŧ
default:
    // āϝāĻĻāĻŋ āĻ•āĻŋāϛ⧁āχ āύāĻž āĻŽā§‡āϞ⧇
}

✅ āωāĻĻāĻžāĻšāϰāĻŖ:

Go
package main

import "fmt"

func main() {
    day := "Monday"

    switch day {
    case "Monday":
        fmt.Println("āϏāĻĒā§āϤāĻžāĻšā§‡āϰ āĻļ⧁āϰ⧁!")
    case "Friday":
        fmt.Println("āϏāĻĒā§āϤāĻžāĻšāĻžāĻ¨ā§āϤ āĻ•āĻžāĻ›āĻžāĻ•āĻžāĻ›āĻŋ!")
    default:
        fmt.Println("āϏāĻžāϧāĻžāϰāĻŖ āĻĻāĻŋāύāĨ¤")
    }
}

🔹 āφāωāϟāĻĒ⧁āϟ:

Bash
āϏāĻĒā§āϤāĻžāĻšā§‡āϰ āĻļ⧁āϰ⧁!

2. āĻāĻ•āĻžāϧāĻŋāĻ• āĻŽāĻžāύ āĻāĻ•āϏāĻžāĻĨ⧇ āĻšā§‡āĻ• āĻ•āϰāĻž

Go
case "Saturday", "Sunday":
    fmt.Println("āĻāϟāĻŋ āϛ⧁āϟāĻŋāϰ āĻĻāĻŋāύ!")

✅ āωāĻĻāĻžāĻšāϰāĻŖ:

Go
day := "Sunday"
switch day {
case "Saturday", "Sunday":
    fmt.Println("āĻāϟāĻŋ āϛ⧁āϟāĻŋāϰ āĻĻāĻŋāύ!")
default:
    fmt.Println("āĻāϟāĻŋ āĻ•āĻ°ā§āĻŽāĻĻāĻŋāĻŦāϏāĨ¤")
}

🔹 āφāωāϟāĻĒ⧁āϟ:

Bash
āĻāϟāĻŋ āϛ⧁āϟāĻŋāϰ āĻĻāĻŋāύ!

3. Switch-āĻāϰ āĻŽāĻ§ā§āϝ⧇ fallthrough

Go
num := 1
switch num {
case 1:
    fmt.Println("Case 1")
    fallthrough
case 2:
    fmt.Println("Case 2")
}

🔹 āφāωāϟāĻĒ⧁āϟ:

Bash
Case 1
Case 2

4. āϟāĻžāχāĻĒ āĻšā§‡āĻ•āĻŋāĻ‚ (type switch)

Go
func typeCheck(i interface{}) {
    switch i.(type) {
    case int:
        fmt.Println("āĻāχāϟāĻŋ āĻāĻ•āϟāĻŋ āϏāĻ‚āĻ–ā§āϝāĻžāĨ¤")
    case string:
        fmt.Println("āĻāχāϟāĻŋ āĻāĻ•āϟāĻŋ āĻ¸ā§āĻŸā§āϰāĻŋāĻ‚āĨ¤")
    }
}

✅ āϏāĻ‚āĻ•ā§āώ⧇āĻĒ⧇:

  • switch āĻ…āύ⧇āĻ• if-else āĻ¸ā§āĻŸā§‡āϟāĻŽā§‡āĻ¨ā§āϟāϕ⧇ āϏāĻšāϜ āĻ•āϰ⧇āĨ¤
  • fallthrough āĻļ⧁āϧ⧁āĻŽāĻžāĻ¤ā§āϰ āĻĒāϰāĻŦāĻ°ā§āϤ⧀ case āϚāĻžāϞāĻžāύ⧋āϰ āϜāĻ¨ā§āϝ āĻŦā§āϝāĻŦāĻšā§ƒāϤ āĻšāϝāĻŧāĨ¤
  • āϟāĻžāχāĻĒ āĻšā§‡āĻ• āĻ•āϰāϤ⧇ type switch āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰāĻž āϝāĻžāϝāĻŧāĨ¤

🚀 Go-āϤ⧇ switch āĻŦā§āϝāĻŦāĻšāĻžāϰ āĻ•āϰ⧁āύ āφāϰāĻ“ āĻ­āĻžāϞ⧋ āĻĒāĻžāϰāĻĢāϰāĻŽā§āϝāĻžāĻ¨ā§āϏ⧇āϰ āϜāĻ¨ā§āϝ!

Š2025 Linux Bangla | Developed & Maintaind by Linux Bangla.