Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

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")
  }
}

Result:

Thursday
Try it Yourself »


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")
  }
}

Result:

Not a weekday
Try it Yourself »

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")
  }
}

Result:

./prog.go:11:2: cannot use "b" (type untyped string) as type int
Try it Yourself »

Go Exercises

Test Yourself With Exercises

Exercise:

Insert the missing parts to complete the following switch statement.

package main   
import ("fmt") 
func main() { var day = 2 switch { (1): fmt.Print("Saturday") (2): fmt.Print("Sunday") } }

Start the Exercise


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.