Switch Cases in Go

May 7, 2021

Course Instructor: Elliot Forbes

Hey Gophers! My name is Elliot and I'm the creator of TutorialEdge and I've been working with Go systems for roughly 5 years now.


Now, in the last video, we covered the topic of if statements in go, and we created an application that allows us to check, to see if a customer height or customer age is above a certain amount. In this case, it was 150 or 18, and then they could access all of the rides otherwise, if they were only above the height of 120, then they could access the children’s rides.

Now, in this video we’re going to clean this code up slightly and change it to use switch statements.

Cool. So the first thing we want to do, if we want to convert this code to use switch statements is to switch with an expression so we can do switch and then open curly brace. And then we can set out the cases that we wish to execute upon.

So the first case is going to be whether or not a customer height is greater than, or equal to 150 or the customer age is greater than, or equal to 18, then we want to println("Can Access Ride").

In the case that the customer height is greater than 120, then println("can access children's rides") and then we can use what’s called the default case to then catch all other instances that don’t meet the criteria for the original two cases that we’ve got here.

And in this case, we want to do println("cannot access ride").

Cool. So let’s comment out to the existing if statements and let’s try and run that by doing go run main.go.

And as you can see, it’s printed out that we can access the ride, and this is because the age of our customer is greater than, or equal to 18.

Now, practically speaking, there isn’t much difference between the two. if/else statements that we have here and the switch case that we’ve got up here. However, as our application tends to grow in complexity, having a switch statement is preferential, as it allows us to more easily reason with the code that we are developing.

Now, aside from replacing a complicated if/else statement with a switch that has no expression, we can also switch based on the output of an expression.

Now, one of the cool examples from the tour of Golang is switching based on the operating system. So let’s implement that now.,

Now, we can assign the variable operating system to equal runtime.GOOS like, so, and then we can switch based on the value of that operating system variable.

So in the case that this is equal to Darwin, Then we want to do println("OSX") and the case that we are on the Linux println("linux machine"), and the default case. So if you’re on windows then println("something else") like so, cool.

Now, one thing that we have not yet covered is the use of the standard library and the packages within that standard library.

Now I’m going to cover this more in more detail in a future video. However, for now you’ll note at the top we have now added an import to the runtime package. Now this import is going to be crucial for the code to work. So please make sure that if you’re following along and you’ve added this import at the top.

Cool. So let’s go down in to the terminal and try and run this application now. So go run main.go - as you can see, it has printed out from our original switch statement and then the second switch statement has then been executed. So it’s assigned the variable string OS to equal the package runtime.go operating system string, and then based on the value of that operating system string, we then switch.

Cool. So in this video, we have looked at the switch statement in go and we’ve covered how we can replace complicated if/else statements with a stateless or an expressionless switch statement. And we’ve also looked at how we can switch based on the value of a variable.


package main

import "runtime"

func main() {
	println("Switch Statements in Go")
	var customerHeight int = 140
	customerAge := 18

	switch {
	case customerHeight >= 150 || customerAge >= 18:
		println("can access ride")
	case customerHeight >= 120:
		println("can access children's rides")
		println("cannot access rides")

	switch os := runtime.GOOS; os {
	case "darwin":
		println("os x")
	case "linux":
		println("linux machine")
		println("something else")



$ go run main.go
Switch Statements in Go
can access ride
os x