Control flow with switch

The switch statement runs the first case which is true.

Pseudo code

switch expression {
    case expression1: statement1
    case expression2: statement2
    ...
    case expressionn: statementn
    default: defaultstatement
}

Runnable example

package main

import (
        "fmt"
)

func main() {
        comment := map[string]string{
                "route53":       "Love it",
                "s3":            "",
                "ec2":           "oh!",
                "Snowball Edge": "Urban Dictionary both of those words.",
                "fargate":       "Sounds clever, but nobody will know what it does.",
                "graviton":      "Terrific if the next word in the product name is 'bomb'",
        }

        // Uncomment one on these three lines
        name := "route53"
        // name := "ec2"
        // name := "s3"
        l := len(comment[name])
        switch {
        case l > 4:
                fmt.Println("Clever comment for: ", name, " - ", comment[name])
        case l > 0:
                fmt.Println("Short comment for: ", name)
        default:
                fmt.Println("No comment for: ", name)
        }

}
 1 package main
  2
  3 import (
  4         "fmt"
  5 )
  6
  7 func main() {
  8         comment := map[string]string{
  9                 "route53":       "Love it",
 10                 "s3":            "",
 11                 "ec2":           "oh!",
 12                 "Snowball Edge": "Urban Dictionary both of those words.",
 13                 "fargate":       "Sounds clever, but nobody will know what it does.",
 14                 "graviton":      "Terrific if the next word in the product name is 'bomb'",
 15         }
 16
 17         // Uncomment one on these three lines
 18         name := "route53"
 19         // name := "ec2"
 20         // name := "s3"
 21         l := len(comment[name])
 22         switch {
 23         case l > 4:
 24                 fmt.Println("Clever comment for: ", name, " - ", comment[name])
 25         case l > 0:
 26                 fmt.Println("Short comment for: ", name)
 27         default:
 28                 fmt.Println("No comment for: ", name)
 29         }
 30
 31 } 

If none of the above conditions (line 23 or line 25) is true, then default (line 27) is run.

You don’t need a break statement, which exits the switch block. But you may use on. If you want to continue the flow inside the switch statement, you may use the fallthrough keyword.

There are some variants to switch:

Switch with constant values

 32         switch l {
 33         case 7:
 34                 fmt.Println("Comment with 7 characters: ", name, " - ", comment[name])
 35         case 0:
 36                 fmt.Println("No comment for: ", name)
 37         }

In line 32 you define the variable which the values in line 33 and 35 are compared to.

Switch with initialized variable

 39         switch u := allUppercase(comment[name]); u {
 40         case true:
 41                 fmt.Println("You dont have to shout")
 42         case false:
 43                 fmt.Println("ok")
 44         default:
 45                 fmt.Println("This will never happen")
 46         }

The default case in line 44/45 is not reachable. This is usually detected by GO.

How would you create the function allUppercase? See an example in the source code.

See also

Source

See the full source on github.

Sources