Concise Introduction to Switch Statement

GoGoBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

The switch statement is a conditional statement that allows you to execute different code blocks based on the value of an expression. It is a powerful tool that can simplify your code and make it more readable.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL go(("`Go`")) -.-> go/FunctionsandControlFlowGroup(["`Functions and Control Flow`"]) go/FunctionsandControlFlowGroup -.-> go/switch("`Switch`") subgraph Lab Skills go/switch -.-> lab-15436{{"`Concise Introduction to Switch Statement`"}} end

Switch

In this challenge, you need to complete the switch statement to print out the corresponding message based on the input value.

Requirements

  • The switch statement must be used to solve the problem.
  • The default case must be used to handle unexpected input values.

Example

$ go run switch.go
Write 2 as two
It's a weekday
It's after noon
I'm a bool
I'm an int
Don't know type string

Summary

In this challenge, you learned how to use the switch statement to execute different code blocks based on the value of an expression. You also learned how to handle unexpected input values using the default case.

Other Go Tutorials you may like