Web Development

Switch Statements are common in programming languages. Go Language has this as well.

Go Language Code

Something you may notice from this switch statement is that you can add additional comparisons per case.

package main

import "fmt"

func main() {
  var cake string = "blueberry cheesecake"
  switch cake {
  case "blueberry cheesecake", "cheesecake":
    fmt.Println("Blueberry Cheesecake")
  case "rocky road":
    fmt.Println("Rocky Road")
  default:
    fmt.Printf("Nope. Not anything we know!")
  }
}

Output

$ go run switch.go 
Blueberry Cheesecake

Notes

  • you can add more than 1 comparison per case
  • default is optional