Tutorialsteacher

Follow Us

Articles
  • C#
  • C# OOP
  • ASP.NET Core
  • ASP.NET MVC
  • LINQ
  • Inversion of Control (IoC)
  • Web API
  • JavaScript
  • TypeScript
  • jQuery
  • Angular 11
  • Node.js
  • D3.js
  • Sass
  • Python
  • Go lang
  • HTTPS (SSL)
  • Regex
  • SQL
  • SQL Server
  • PostgreSQL
  • MongoDB
  • Go lang - Get Started
  • Go lang Overview
  • Install Go
  • Go Syntax
  • Create Go Program
  • Go DataTypes
  • Go Variables
  • Go Operators
  • Go if-else Statement
  • Go Switch Statement
  • Go For Loop
  • Working with Functions
  • Go Arrays
  • Go Slice
  • Go Struct
  • Go Map
  • Go Pointers
  • Go Packages
  • Go Modules
Entity Framework Extensions - Boost EF Core 9
  Bulk Insert
  Bulk Delete
  Bulk Update
  Bulk Merge

Go Switch Statements

The switch statement is a shorter way of writing an if-else statement. It allows you to execute one among many code blocks based on a condition. It runs the first case where the expression matches the value.

The Go switch statement is similar to the switch statements in other programming languages such as C#, C++, and java.

In Go, the switch statements can have a single case or multiple case values for each case.

In single value switch statements, each case will have a single value to be compared with the expression, as shown in the following example.

Syntax
switch expression {
	case 1:
		//code block 1
	case 2:
		//code block 2
	case 3:
		//code block 3
	default:
		//default code block
}

In the above syntax, the expression is evaluated once. Its value is compared with the value of each case. When the expression value matches the case value, the code block of that case is executed. If none of the cases matches the expression then the default code block is executed.

In the following example, the month is passed as a number and the case displays the actual month in words.

Example: Switch
package main
import "fmt"

func main() {
	thisMonth := 5

	switch thisMonth {
		case 1:
			fmt.Println("January")
		case 2:
			fmt.Println("February")
		case 3:
			fmt.Println("March")
		case 4:
			fmt.Println("April")
		case 5:
			fmt.Println("May") 
		case 6:
			fmt.Println("June")
	}  
}
Try it
Output:
May

The following example contains multiple values in each case.

Example: Switch with Multiple Case Values
package main
import "fmt"
	 
func main() {
	thisMonth := 6

	switch thisMonth {
		case 1, 3, 5, 7, 8, 10,12:
			fmt.Println("31 days")
		case 4,6,9,11:
			fmt.Println("30 days")
		case 2:
			fmt.Println("28 days")
	}  
}
Try it
Output:
30 days

The switch statements can be used with any datatype.

Example: Switch
package main
import "fmt"

func main() {
	thisMonth := "May"

	switch thisMonth {
		case "January":
			fmt.Println("1")
		case "February":
			fmt.Println("2")
		case "March":
			fmt.Println("3")
		case "April":
			fmt.Println("4")
		case "May":
			fmt.Println("5") 
		case "June":
			fmt.Println("6")
	}  
}
Try it
Output:
5

Conditional Case

The comparison operators can be used in the switch case statements, as shown below.

Example: Conditional Cases
package main
import "fmt"
	 
func main() {
    x := 20
    y := 10 

    switch {
	    case x > y:
		    fmt.Println("x is greater than y")
	    case x < y:
		    fmt.Println("x is less than y")
	    default:
		    fmt.Println("x is equal to y")
    }  
}
Try it
Output:
x is greater than y

Fallthrough Case Statement

The fallthrough keyword in the case statement transfers the execution to the next case.

Example:
package main
import "fmt"
	 
func main() {
    x := 3 

    switch x {
	    case 1:
		    fmt.Println("1")
		    fallthrough
	    case 3:
		    fmt.Println("3")
		    fallthrough
	    case 5:
		    fmt.Println("5")
	}
}
Try it
Output:
3 5

Go Switch Initializer Statement

Like for loop and if statement, the switch statement can start with the short statement. For example, a variable local to the switch block can be declared and initialized immediately after the switch keyword, as shown below.

Example: Short Statement with Switch
package main
import "fmt"
	 
func main() {
    
    switch day:= 4; 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")
	    default:
		    fmt.Println("Sunday")
    }  
}
Try it
Output:
Thursday

In the above example, a variable day is declared and initialized immediately after the switch keyword. It is also passed as the expression to the switch block. It is local to the switch block where it is declared.

TUTORIALSTEACHER.COM

TutorialsTeacher.com is your authoritative source for comprehensive technologies tutorials, tailored to guide you through mastering various web and other technologies through a step-by-step approach.

Our content helps you to learn technologies easily and quickly for learners of all levels. By accessing this platform, you acknowledge that you have reviewed and consented to abide by our Terms of Use and Privacy Policy, designed to safeguard your experience and privacy rights.

[email protected]

ABOUT USTERMS OF USEPRIVACY POLICY
copywrite-symbol

2024 TutorialsTeacher.com. (v 1.2) All Rights Reserved.