Go · Stage 3 – Making Decisions · Lesson 23 of 64

switch Statements

Choose among several clear value-based cases.

Go code

day := "Monday"
switch day {
case "Monday":
    fmt.Println("Start")
case "Friday":
    fmt.Println("Finish")
default:
    fmt.Println("Middle")
}
Line-by-line explanation
  1. Day stores Monday.
  2. Switch examines day.
  3. The Monday case matches.
  4. Start is displayed.
  5. Go stops after that case automatically.
Expected output
Start

This is the expected result.

Unlike some languages, ordinary Go cases do not need break to prevent automatic fall-through.