Switch in Julia

The instructions provided suggest translating Go code into Julia. Here is the translated content in Markdown format suitable for Hugo:

Switch statements express conditionals across many branches.

function main()
    i = 2
    println("Write ", i, " as ")

    # Here's a basic switch equivalent in Julia
    if i == 1
        println("one")
    elseif i == 2
        println("two")
    elseif i == 3
        println("three")
    end

    # Multiple expressions in the same if-elseif statement and using a default behaviour
    day = Dates.dayname(Dates.now())
    if day == "Saturday" || day == "Sunday"
        println("It's the weekend")
    else
        println("It's a weekday")
    end

    # Switch without an expression is similar to a series of if-elseif statements
    t = Dates.now()
    if Dates.hour(t) < 12
        println("It's before noon")
    else
        println("It's after noon")
    end

    # A type switch compares types instead of values
    function whatAmI(i)
        if isa(i, Bool)
            println("I'm a bool")
        elseif isa(i, Int)
            println("I'm an int")
        else
            println("Don't know type ", typeof(i))
        end
    end

    whatAmI(true)
    whatAmI(1)
    whatAmI("hey")
end

main()

To run the program, save the code in a file like switch_example.jl and use julia to execute it.

$ julia switch_example.jl
Write 2 as 
two
It's a weekday
It's after noon
I'm a bool
I'm an int
Don't know type String

Now that we can run and test switch-based logic in Julia, let’s learn more about the language.