From the course: Learning Go

Evaluate expressions with switch statements - Go Tutorial

From the course: Learning Go

Evaluate expressions with switch statements

- [Instructor] Go switch statement serves the same purpose as an other C-style languages, but its syntax expands on those languages. For this demonstration, I'm starting with a randomized value. I'm using a package called math slash Rand and another package the time package that we've seen before. From the rand package, I'm calling the seed function and passing in the current time in Unix format. Then I'm using a function called Intn and passing in a ceiling of seven, and then adding one, and this will give me a number between one and seven. Each time I run the application, I'll get possibly a different number. And it all depends on the millisecond of the current time on my computer. It is possible to see the same number over and over again. But that's just coincidence. If I ran the same code in the go playground online, I would always get the same number, because in that environment, it's always the same date and time. Now, I'll show you how to use the switch statement to evaluate the number that's generated. I'll start with the switch keyword, and then dow, the variable that I declared. Just like with the if statement, you don't need parentheses around the expression that you were evaluating. Above the switch statement, I'll declare a variable named result and I'll say that it's going to be a string. And now I'm ready for my cases. I'll start with case one and then I'll set result to it's Sunday. Then case two, and I'll say it's Monday, and so on, and so forth. And then I could add a default statement if I like. And I'll set the result to it's some other day. Then at the end of this switch statement, I'll output the result. And I'll run the code a few different times to see different results. Now notice there's something different here from C or Java, there is no use of the break command. In Go, as soon as one of these cases is evaluated as true, it will execute the code in that case, and then jump to the end of the switch statement. Just like in the if statement, you can include a short statement before the evaluation of your variables. So for example, I can take this statement, and cut and paste it and put it here after the switch key word, and then separated from everything else with the semicolon. I no longer need this line of code. So I'll comment it out. And I'll run the code again. And again, I'm seeing the results. If you prefer to use C-style flow, that is where the break statement is required to prevent falling through to other cases, you can restore it with Go's fall through keyword. If you add fall through after any code within a case, if that case is true, you'll execute its code but you will also execute the next case. So, if I had fall through here, and also after this one, I'm now using the flow the same way I wouldn't see in Java. And let's see what happens. This time, no matter how many times I run the application, I keep on getting it some other day. And that's because of the fall through keyword. That's not what I want. So I'm going to comment those uses of the fall through keyword out. I'll run the code a few more times. And eventually, I'll get a Sunday or a Monday. So that's the switch statement in Go. So once again, here are some differences. First, you don't wrap parentheses around the value that you're evaluating. You can add a statement before the evaluation and any variables declared within that statement will be local to the switch statement. And you don't need to add the break keywords within each case. But if you want that style of flow, you can use the fall through keyword.

Contents