Switch Statement in Go

Golang like most other programming languages ​​has the switch statement . The switch statement in Golang  allows you to evaluate a variable or expression in multiple cases and is often used when writing multiple if-else statements makes the code look ugly and repetitive.

Switch Statement in Go

In Go, a switch statement is a multi-directional branching statement that effectively directs execution based on the value (or type) of an expression. There are two main types of switch statements in Go :

  • Expression Switch
  • Type Switch

For example:

package main
import "fmt"

func main() {
    day := 4
    switch day {
    case 1:
        fmt.Println("Monday")
    case 2:
        fmt.Println("Tuesday")
    case 3:
        fmt.Println("Wednesday")
    case 4:
        fmt.Println("Thursday")
    case 5:
        fmt.Println("Friday")
    default:
        fmt.Println("Invalid day")
    }
}

Switch statement syntax in Golang

switch optstatement; optexpression {
case expression1:
    // Khối code
case expression2:                                                          # Expression Switch
    // Khối code
default:
    // Khối code
}

switch var := interfaceValue.(type) {
case type1:
    // Khối code
case type2:                                                                    # Type Switch
    // Khối code
default:
    // Khối code
}

Expression Switch

Expression Switch evaluates an expression and switches to a case based on the value of that expression. If no expression is provided, switch defaults to true .

Syntax

switch optstatement; optexpression {
case expression1:
    // Khối code
case expression2:
    // Khối code
default:
    // Khối code
}

optstatement: Optional statement (e.g. variable declaration).
optexpression: Optional expression (if omitted, defaults to true ).

Example with optional command

Here we introduce an optional statement that declares a variable day . The switch statement then evaluates day under various cases.

package main
import "fmt"
func main() {
    switch day := 4; day {
    case 1:
        fmt.Println("Monday")
    case 2:
        fmt.Println("Tuesday")
    case 3:
        fmt.Println("Wednesday")
    case 4:
        fmt.Println("Thursday")
    case 5:
        fmt.Println("Friday")
    default:
        fmt.Println("Invalid day")
    }
}

Result:

Thursday

Example with optional Expression

If no expression is specified, the switch statement in Golang will assume the expression is true. This allows us to use boolean conditions in case statements.

package main
import "fmt"

func main() {
    day := 4

    switch {
    case day == 1:
        fmt.Println("Monday")
    case day == 4:
        fmt.Println("Thursday")
    case day > 5:
        fmt.Println("Weekend")
    default:
        fmt.Println("Invalid day")
    }
}

Result

Thursday

Type Switch

Type Switch is used to branch on the type of an interface value, rather than its value. This is especially useful when dealing with variables of unknown type.

Syntax

switch var := interfaceValue.(type) {
case type1:
    // Khối code
case type2:
    // Khối code
default:
    // Khối code
}

For example:

This example uses the same day variable but wrapped in interface{} to illustrate type casting.

package main
import "fmt"
func main() {
    var day interface{} = 4
    switch v := day.(type) {
    case int:
        switch v {
        case 1:
            fmt.Println("Monday")
        case 2:
            fmt.Println("Tuesday")
        case 3:
            fmt.Println("Wednesday")
        case 4:
            fmt.Println("Thursday")
        case 5:
            fmt.Println("Friday")
        default:
            fmt.Println("Invalid day")
        }
    default:
        fmt.Printf("Unknown type: %T\n", v)
    }
}

Result:

Thursday

Leave a Comment

Pop Culture Costume Ideas That Will Dominate Halloween 2026

Pop Culture Costume Ideas That Will Dominate Halloween 2026

Plan a recognizable Halloween 2026 look with pop culture costumes inspired by Spider-Man, Supergirl, Toy Story 5, Moana, Mario, and Wednesday.

Leftover Halloween Candy and Pumpkins: What to Do Next

Leftover Halloween Candy and Pumpkins: What to Do Next

Sort leftover Halloween candy safely, share or repurpose sealed treats, and choose food use or compost for pumpkins based on their condition.

What Not to Wear: A Guide to Professional Workplace Halloween Etiquette

What Not to Wear: A Guide to Professional Workplace Halloween Etiquette

Compare workplace Halloween costume choices by respect, dress code, mobility, and safety—and learn when a subtle accessory or a policy check is the better option.

Couples Costume Ideas for 2026: Iconic Pairs and Clever Tropes

Couples Costume Ideas for 2026: Iconic Pairs and Clever Tropes

Find a couples costume that fits your style, budget, and comfort level, from Addams Family classics to original inside-joke duos.

Last-Minute High School Halloween Costumes That Take 5 Minutes

Last-Minute High School Halloween Costumes That Take 5 Minutes

Need a last-minute high school Halloween costume? Compare 10 easy closet-based ideas by prep time, comfort, school-day practicality, and simple safety checks.

When to Start Decorating for Halloween in 2026

When to Start Decorating for Halloween in 2026

Use a simple 2026 Halloween decorating timeline for indoor décor, outdoor displays, and fresh pumpkins, with practical adjustments for weather and events.

5 Halloween Cocktails That Are Easy to Serve at Your 2026 Bash

5 Halloween Cocktails That Are Easy to Serve at Your 2026 Bash

Mix five vivid, approachable Halloween cocktails with measured recipes, simple prep tips, and an alcohol-free option for your party guests.

15 HR-Approved Office Halloween Costumes for 2026

15 HR-Approved Office Halloween Costumes for 2026

Find 15 office-friendly Halloween costume ideas for 2026, with practical checks for dress codes, respectful themes, comfort, and workplace safety.

Spooky but Sweet: Halloween Party Ideas for Elementary Schools

Spooky but Sweet: Halloween Party Ideas for Elementary Schools

Plan a fun elementary school Halloween party with easy crafts, games, inclusive treats, costume tips, and a sample 75-minute classroom schedule.

Side Hustles That Actually Pay in 2026: 7 Realistic Options and the Tradeoffs to Know

Side Hustles That Actually Pay in 2026: 7 Realistic Options and the Tradeoffs to Know

Compare realistic side hustles for 2026 by startup cost, fees, flexibility, and income control, with current tax and scam guidance from official sources.