bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/Go/Go Tutorial
Go•Go Tutorial

Go switch Statement

The switch Statement

Use the switch statement to select one of many code blocks to be executed.

The switch statement in Go is similar to the ones in C, C++, Java, JavaScript, and PHP. The difference is that it only runs the matched case so it does not need a break statement.

Single-Case switch Syntax

Syntax

switch
expression {
 case
 x
 // code block
 case
 y
 // code block
 case
 z
 ...
 default:
 // code block
}

This is how it works

  • The expression is evaluated once
  • The value of the switch expression is compared with the values of each case
  • If there is a match, the associated block of code is executed
  • The default keyword is optional. It specifies some code to run if there is no case match

Single-Case switch Example

The example below uses a weekday number to calculate the weekday name:

Example

package main
import ("fmt")
func main() {
  day := 4
  switch day {
    case 1: fmt.Println("Monday")
    case 2: fmt.Println("Tuesday")
    case 3: fmt.Println("Wednesday")
    case 4: fmt.Println("Thursday")
    case 5: fmt.Println("Friday")
    case 6: fmt.Println("Saturday")
    case 7: fmt.Println("Sunday")
  }
}

The default Keyword

The default keyword specifies some code to run if there is no case match:

Example

package main
import ("fmt")
func main() {
  day := 8
  switch day {
    case 1: fmt.Println("Monday")
    case 2: fmt.Println("Tuesday")
    case 3: fmt.Println("Wednesday")
    case 4: fmt.Println("Thursday")
    case 5: fmt.Println("Friday")
    case 6: fmt.Println("Saturday")
    case 7: fmt.Println("Sunday")
    default: fmt.Println("Not a weekday")
  }
}

All the case values should have the same type as the switch expression. Otherwise, the compiler will raise an error:

Example

package main
import ("fmt")
func main() {
  a := 3
  switch a {
    case 1: fmt.Println("a is one")
    case "b": fmt.Println("a is b")
  }
}

Previous

Go Nested if Statement

Next

Go Multi-case switch Statement

This chapter

Overview
39

Lessons

95m

Read time

1. Go Tutorial2. Go Introduction3. Go Getting Started4. Go Syntax5. Go Comments6. Go Variables7. Go Multiple Variable Declaration8. Go Variable Naming Rules9. Go Constants10. Go Output Functions11. Go Formatting Verbs12. Go Data Types13. Go Boolean Data Type14. Go Integer Data Types15. Go Float Data Types16. Go String Data Type17. Go Arrays18. Go Slices19. Go Access, Change, Append and Copy Slices20. Go Operators21. Go Arithmetic Operators22. Go Assignment Operators23. Go Comparison Operators24. Go Logical Operators25. Go Bitwise Operators26. Go Conditions27. Go if statement28. Go if else Statement29. Go else if Statement30. Go Nested if Statement31. Go switch Statement32. Go Multi-case switch Statement33. Go For Loops34. Go Functions35. Go Function Parameters and Arguments36. Go Function Returns37. Go Recursion Functions38. Go Struct39. Go Maps

On this page

The switch StatementSingle-Case switch SyntaxSingle-Case switch ExampleThe default Keyword