This course has been updated! We now recommend you take the Basics of Go course.
Transcript from the "Control Structures: Switch Statements" Lesson
[00:00:00]
>> The next subject I'd like to talk a little bit about is switch statements which will look really similar to what you're used to seeing in JavaScript. I'm gonna hop back over to my code editor and find the switch.go file. Once again, I'm going to uncomment the whole file for now, and start at the top.
[00:00:24] So in our main function, there are a few different styles of switch statements that mainly depend on what we're starting with in terms of a variable. So our first example here, we have a switch expression with the variable called city. And so that's looking at a variable defined on line 9, we're resetting this to a string, doesn't have a value yet but imagine down the road it was assigned to something specific.
[00:00:46] And then our case statements are going to check and see if the variable city equals one of the case strings as an example. So if city equals Des Moines, print the line you live in Iowa. An interesting thing about switch statements and go is it does allow you to have kind of an or, so if the city equals either Minneapolis or St Paul, you live in Minnesota.
[00:01:06] And if the string equals Madison, you live in Wisconsin. And there's a default case statement of a if none of these match, what do you wanna do about it, and in this case, we print the line, you're not from around here. And so the main difference that I would like to point out just, again, is that ability to have kind of like an order structure, like if either of these things are equal, go ahead and execute this code.
[00:01:34] The second structure is missing that original variable here. So you'll notice that switch doesn't have a variable to the right of it. But it does have a variable that's been defined elsewhere at the top. So here we're working with var i which is set to some integer. And then within the case statements, we're now looking at a conditional.
[00:01:52] Because we're not checking for strict equality between like the variable city and the string Des Moines. We're now looking to see if integer the variable i is greater than 10, less than 10. And there's that default statement in case none of those match once again. And the last thing I wanna bring up around switch statements has to do with the fall through.
[00:02:17] And so, go will not automatically continue through the case statements. Once it matches 1, it will stop executing code. Unless you include this fall through line, which is on line 40. So in this case, we have our i set to a value of 9. And so, if we're looking at our switch statement here, that first line on 38, the case i does not equal 10 would return true.
[00:02:39] So we'd execute the code that's on line 39, we'd print the line does not equal 10. But because that fall through line is there go is gonna keep going. It's gonna execute that next case statement and check to see if i is also less than 10, greater than 10, and then the default.
[00:02:52] So it's a way to control whether or not you want your case statement to continue executing code once it reaches something that returns true.