I am learning Golang, was going through the tour where I found a tutorial on Switch evaluation order. I modified it a bit (e.g. Saturday to Sunday), just to play around. It printed Too far away. even for Sunday. So, I modified the code to look like this:
package main
import (
"fmt"
"time"
)
func main() {
day := time.Monday
fmt.Printf("When's %v?
", day)
today := time.Now().Weekday()
switch day {
case today + 0:
fmt.Println("Today.")
case today + 1:
fmt.Println("Tomorrow.", today + 1)
case today + 2:
fmt.Println("In two days.", today + 2)
default:
fmt.Println("Too far away.", today + 2)
}
}
Now, it gives me the output:
When's Monday?
Too far away. %!v(PANIC=runtime error: index out of range)
What can I do to MOD the index, instead of adding it beyond array? Seems to me like some kind of operator overloading. Shouldn't it do MOD, on add operation, by default in case of days, at least?