Code with Go
Ask or search…


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 (
func main() {
// Example 1: Basic switch statement with an integer
i := 2
fmt.Print("Write ", i, " as ")
switch i {
case 1:
case 2:
case 3:
// Example 2: Switch statement with time.Weekday
switch time.Now().Weekday() {
case time.Saturday, time.Sunday:
fmt.Println("It's the weekend")
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")
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")
fmt.Printf("Don't know type %T\n", t)
  1. 1.
    Basic Switch with Integer (i):
    • The code initializes an integer i with the value 2.
    • The switch statement checks the value of i and prints the corresponding word for the number.
  2. 2.
    Switch with time.Weekday:
    • The code uses the time.Now().Weekday() function to get the current day of the week.
    • The switch statement checks if it's Saturday or Sunday and prints a message accordingly.
  3. 3.
    Switch without Expression (t.Hour()):
    • The code uses the current time (time.Now()) and checks the hour using t.Hour().
    • The switch statement doesn't have a specific expression but evaluates conditions based on the hour.
  4. 4.
    Switch with Type Assertion (whatAmI function):
    • The code defines a function whatAmI that takes an empty interface (interface{}), allowing it to accept values of any type.
    • Inside the function, a switch statement uses type assertion (i.(type)) to determine the type of the input and prints a message accordingly.
    Example outputs:
    • I'm a bool for true
    • I'm an int for 1
    • Don't know type string for "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.


Write 2 as two
It's a weekday
It's before noon
I'm a bool
I'm an int
Don't know type string
Last modified 1mo ago