Keyword

Go switch keyword

Selects a matching branch; expression switches normally stop after the chosen clause unless fallthrough is explicit. An omitted expression behaves like true. Type switches choose by dynamic type and cannot fall through. Example prints medium.

Formal Syntax & Structure

go syntaxgo
switch [init;] [expression] { case expressions: statements; default: statements } | switch v := x.(type) { case Type: statements }

Working Example

Example Snippetgo
package main
import "fmt"
func main() {  switch size := 5; {  case size < 3:    fmt.Println("small")  case size < 8:    fmt.Println("medium")  default:    fmt.Println("large")  }}
Go Reference