Switch
The provided Go code demonstrates the usage of the switch statement in various scenarios.
Let's break down the code step by step:
package main
import (
"fmt"
"time"
)
func main() {
// Example 1: Basic switch statement with an integer
i := 2
fmt.Print("Write ", i, " as ")
switch i {
case 1:
fmt.Println("one")
case 2:
fmt.Println("two")
case 3:
fmt.Println("three")
}
// Example 2: Switch statement with time.Weekday
switch time.Now().Weekday() {
case time.Saturday, time.Sunday:
fmt.Println("It's the weekend")
default:
fmt.Println("It's a weekday")
}
// Example 3: Switch statement without an expression
// Evaluates the current time and checks if it's before or after noon
t := time.Now()
switch {
case t.Hour() < 12:
fmt.Println("It's before noon")
default:
fmt.Println("It's after noon")
}
// Example 4: Switch statement with a type assertion
// Uses a function literal (anonymous function) to determine the type of the input
whatAmI := func(i interface{}) {
switch t := i.(type) {
case bool:
fmt.Println("I'm a bool")
case int:
fmt.Println("I'm an int")
default:
fmt.Printf("Don't know type %T\n", t)
}
}
whatAmI(true)
whatAmI(1)
whatAmI("hey")
}Explanation:
Basic Switch with Integer (
i):The code initializes an integer
iwith the value 2.The
switchstatement checks the value ofiand prints the corresponding word for the number.
Switch with
time.Weekday:The code uses the
time.Now().Weekday()function to get the current day of the week.The
switchstatement checks if it's Saturday or Sunday and prints a message accordingly.
Switch without Expression (
t.Hour()):The code uses the current time (
time.Now()) and checks the hour usingt.Hour().The
switchstatement doesn't have a specific expression but evaluates conditions based on the hour.
Switch with Type Assertion (
whatAmIfunction):The code defines a function
whatAmIthat takes an empty interface (interface{}), allowing it to accept values of any type.Inside the function, a
switchstatement uses type assertion (i.(type)) to determine the type of the input and prints a message accordingly.
Example outputs:
I'm a boolfortrueI'm an intfor1Don't know type stringfor"hey"
This code showcases the flexibility and power of the switch statement in Go, allowing developers to handle different types of conditions and expressions efficiently.
Output
Write 2 as two
It's a weekday
It's before noon
I'm a bool
I'm an int
Don't know type stringLast updated
Was this helpful?