golang/tour

tour: commas and semicolons in switch case

Opened this issue · 0 comments

Context: https://go.dev/tour/flowcontrol/10

In https://go.dev/tour/flowcontrol/10 we have an example of an if statement with semicolons in the condition. It would be nice to explain that this also works with switch.

Also, we could add an example with commas in a case to demonstrate another way to write cases.

We could replace the code in https://go.dev/tour/flowcontrol/10 with this:

package main

import (
	"fmt"
	"time"
)

func main() {
	fmt.Println("When's Saturday?")
	today := time.Now().Weekday()
	switch saturday := time.Saturday; saturday {
	case today + 0:
		fmt.Println("Today.")
	case today + 1:
		fmt.Println("Tomorrow.")
	case today + 2:
		fmt.Println("In two days.")
	case today + 3, today + 4, today + 5:
		fmt.Println("In 3, 4 or 5 days.")
	default:
		fmt.Println("Too far away.")
	}
}