Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

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:

  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. 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. 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. 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.

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 string
PreviousIf ElseNextArray

Last updated 1 year ago

Was this helpful?